C#中的数据表查询不存在的联接 [英] c# data table query not exist joins

查看:131
本文介绍了C#中的数据表查询不存在的联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#LINQ查询有点糊涂了。我有值的表如下所示

I am bit confused with C# LINQ queries. I have a table with values as shown below

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);



我需要保持值tableold(基表),这是不存在的tableNew(新表)

I need to keep values in tableold (base table) which are not there in tableNew(new table).

所以我需要更新的数据表(tableold)看起来是这样的:

So i need the updated data table (tableold) to look like this :

21, "Combivent", "Janet", "10:20:00"
100, "Dilantin", "Melanie", "10:20:00"

如何在C#或LINQ

how to write such query in c# or LinQ

请帮帮忙!谢谢

推荐答案

试试这个:

 var compare= tableold.AsEnumerable().Select(r => r.Field<int>("Dosage"))
         .Except(tableNew.AsEnumerable().Select(r => r.Field<int>("Dosage")));
 DataTable tblResult= (from row in tableold.AsEnumerable()
                                join id in compare
                                on row.Field<int>("Dosage") equals id
                                select row).CopyToDataTable();

这篇关于C#中的数据表查询不存在的联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆