使用LINQ需要有关内部连接查询的帮助

时间:2022-12-15 01:49:00

I have two tables in an XML Dataset. T1, T2. Each of the tables has a ID column.

我在XML数据集中有两个表。 T1,T2。每个表都有一个ID列。

T1 has a list of Customers T2 has a list of Orders

T1有一个客户列表T2有一个订单列表

I want to build a LINQ query that returns only the ID of the customers that do not have orders. In other words customer ID's that do not exist in the T2 table.

我想构建一个LINQ查询,它只返回没有订单的客户的ID。换句话说,T2表中不存在客户ID。

Oh yea, I'm using C#

哦,是的,我正在使用C#

Thanks!

3 个解决方案

#1


I think this will work (please adapt to your DataSets):

我认为这将有效(请适应您的DataSet):

var query = from c in T1
            where !(from o in T2 select o.CustomerID)
            .Contains(c.CustomerID)
            select c;

#2


This requires an outer join and a check on null.

这需要外部联接和null检查。

var result = from c in Customers
             join d in Details on d.CustomerID equals c.ID into g
             where !g.Any()
             select c;

#3


You just need to us a where clause and all:

你只需要一个where子句和所有:

T1.Where( item1 => T2.All( item2 => item1.ID != item2.ID ) );

#1


I think this will work (please adapt to your DataSets):

我认为这将有效(请适应您的DataSet):

var query = from c in T1
            where !(from o in T2 select o.CustomerID)
            .Contains(c.CustomerID)
            select c;

#2


This requires an outer join and a check on null.

这需要外部联接和null检查。

var result = from c in Customers
             join d in Details on d.CustomerID equals c.ID into g
             where !g.Any()
             select c;

#3


You just need to us a where clause and all:

你只需要一个where子句和所有:

T1.Where( item1 => T2.All( item2 => item1.ID != item2.ID ) );