Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C DataTable查找其他DataTable中不存在的行_C#_Datatable - Fatal编程技术网

C# C DataTable查找其他DataTable中不存在的行

C# C DataTable查找其他DataTable中不存在的行,c#,datatable,C#,Datatable,我有两个数据表。一个代表客户,另一个代表客户联系人 我需要找到所有没有CustomerContact行的Customer行 除了有2个循环并使用DataView和FindRows查找哪些客户没有任何联系人之外,还有更简单的编码方法吗?我不熟悉LINQ,不希望在我当前的项目中使用它 我正在使用.NET4.5 我不能直接在数据库中使用SQL,因为没有数据库。我通过自定义读取Excel文件来填充数据表。您可以使用LINQ实现这一点,如下a&b是两个数据表 var query = (from x in

我有两个数据表。一个代表客户,另一个代表客户联系人

我需要找到所有没有CustomerContact行的Customer行

除了有2个循环并使用DataView和FindRows查找哪些客户没有任何联系人之外,还有更简单的编码方法吗?我不熟悉LINQ,不希望在我当前的项目中使用它

我正在使用.NET4.5


我不能直接在数据库中使用SQL,因为没有数据库。我通过自定义读取Excel文件来填充数据表。

您可以使用LINQ实现这一点,如下a&b是两个数据表

var query = (from x in a.AsEnumerable()
          join y in b.AsEnumerable() on x.Field<int>("col1") equals y.Field<int>("col1")
          select new { col1= y.Field<int>("col1"), col2=x.Field<int>("col2") }).ToList();

请尝试下面的代码。它不使用任何LINQ。将customerID替换为表的实际Id列

DataTable Customers  = new DataTable();//fill this table from db
DataTable CustomersContacts = new DataTable();//fill this table from db
List<string> NoContactCus = new List<string>();
foreach (DataRow Customer in Customers.Rows)
{
    DataRow[] contacts = CustomersContacts.Select(string.Format("customerID={0}", Customer["customerID"].ToString()));
    if (contacts.Count() == 0)
        NoContactCus.Add(Customer["customerID"].ToString());
}
林克是你的朋友

var result = dtCustomers.AsEnumerable().Select(row => (int)row["id"])
    .Except(dtCustomerContacts.AsEnumerable().Select(row => (int)row["customerId"]));
在哪里 dtCustomers和dtCustomerContacts是数据表, id和customerId是主键,外键假定它们是int,
结果是没有任何联系人的客户ID列表。

不是对您问题的回答,但您确实应该开始熟悉LINQ。它已经成为即使是最简单的任务的标准,并且几乎可以在任何代码库中找到。甚至微软也推出了一个例子,就是TypeInfo类及其属性过滤。与编写自己的2个循环和使用FindRows相比,性能会更好还是更差?