Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with an Opposite to Inner join Query using LINQ

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!

like image 439
Rick Avatar asked Mar 09 '09 10:03

Rick


2 Answers

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;
like image 182
Muhammad Hasan Khan Avatar answered Sep 22 '22 21:09

Muhammad Hasan Khan


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;
like image 29
bruno conde Avatar answered Sep 22 '22 21:09

bruno conde