我有两个 DataSet,每个 DataTable 在里面。
数据集#1:
DataTable table = new DataTable(name);
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Customer Name",typeof(string));
table.Columns.Add("Contact Name", typeof(string));
table.Columns.Add("Adress", typeof(string));
table.Columns.Add("City", typeof(string));
table.Columns.Add("Postal Code", typeof(string));
table.Columns.Add("Country", typeof(string));
string mem;
Console.Clear();
Console.WriteLine("| {0} | {1} | {2} | {3} | {4} | {5} |",
table.Columns[1], table.Columns[2], table.Columns[3],
table.Columns[4], table.Columns[5], table.Columns[6]);
int count=1;
do
{
table.Rows.Add(count, Console.ReadLine(), Console.ReadLine(), Console.ReadLine(),
Console.ReadLine(), Convert.ToInt32(Console.ReadLine()), Console.ReadLine());
Console.WriteLine("if finished, type end");
mem=Console.ReadLine();
count++;
}
while (mem != "end");
DataSet set = new DataSet();
set.Tables.Add(table);
和数据集#2:
(“加载”是 DataSet #1)
string srch = Console.ReadLine();
string ask = "[Customer Name] like '%"+srch+"%' OR [Contact Name] like '%"+srch+"%' OR [Adress] like '%"+srch+"%' OR [City] like '%"+srch+"%' OR [Postal Code] like '%"+srch+"%' OR [Country] like '%"+srch+"%'";
DataRow[] foundRows = load.Tables[0].Select(ask);
DataTable dt = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Customer Name",typeof(string));
table.Columns.Add("Contact Name", typeof(string));
table.Columns.Add("Adress", typeof(string));
table.Columns.Add("City", typeof(string));
table.Columns.Add("Postal Code", typeof(string));
table.Columns.Add("Country", typeof(string));
foreach (DataRow item in foundRows)
{
dt.Rows.Add(item.ItemArray);
}
DataSet set = new DataSet();
set.Tables.Add(dt);
将 DataSet #2 发送到函数 Print() 时,在 DataSet #1 的“dtc.AsEnumerable()”处出现错误,一切正常...
static void Print(DataSet load)
{
DataTable dtc = load.Tables[0];
List<int> maxlenght = Enumerable.Range(0, dtc.Columns.Count)
.Select(col => dtc.AsEnumerable()
.Select(row => row[col]).OfType<string>()
.Max(val => val.Length)).ToList();
//rest prints DataTable on a grid in console
}
这怎么可能,而两个数据集在结构上是相同的?