c#数据表查询不存在连接
我对C#LINQ查询感到困惑。 我有一个包含值的表,如下所示
DataTable tableold = new DataTable(); tableold.Columns.Add("Dosage", typeof(int)); tableold.Columns.Add("Drug", typeof(string)); tableold.Columns.Add("Patient", typeof(string)); tableold.Columns.Add("Date", typeof(DateTime)); // Here we add five DataRows. tableold.Rows.Add(25, "Indocin", "David", DateTime.Now); tableold.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); tableold.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); tableold.Rows.Add(21, "Combivent", "Janet", DateTime.Now); tableold.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
现在我有另一张桌子
DataTable tableNew = new DataTable(); tableNew.Columns.Add("Dosage", typeof(int)); // Here we add five DataRows. tableNew.Rows.Add(25); tableNew.Rows.Add(50); tableNew.Rows.Add(10);
我需要在tableNew(新表)中保存tableold(基表)中的值。
所以我需要更新的数据表(tableold)看起来像这样:
21, "Combivent", "Janet", "10:20:00" 100, "Dilantin", "Melanie", "10:20:00"
如何在c#或LinQ中编写这样的查询
请帮忙! 谢谢
试试这个:
var compare= tableold.AsEnumerable().Select(r => r.Field("Dosage")) .Except(tableNew.AsEnumerable().Select(r => r.Field ("Dosage"))); DataTable tblResult= (from row in tableold.AsEnumerable() join id in compare on row.Field ("Dosage") equals id select row).CopyToDataTable();