我的内部每个DataTable都有两个DataSet。
DataSet#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);
和DataSet#2:
(" load"是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()函数时,我在" dtc.AsEnumerable()"中收到错误。 使用DataSet#1一切正常......
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
}
这怎么可能,虽然两个DataSet在结构上是相同的?