7

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

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

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.

Oh yea, I'm using C#

Thanks!

Rick
  • 648
  • 1
  • 11
  • 25

3 Answers3

9

This requires an outer join and a check on null.

var result = from c in Customers
             join d in Details on d.CustomerID equals c.ID into g
             where !g.Any()
             select c;
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
6

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

var query = from c in T1
            where !(from o in T2 select o.CustomerID)
            .Contains(c.CustomerID)
            select c;
bruno conde
  • 47,767
  • 15
  • 98
  • 117
2

You just need to us a where clause and all:

T1.Where( item1 => T2.All( item2 => item1.ID != item2.ID ) );
bstoney
  • 6,594
  • 5
  • 44
  • 51