Question : Fetch records from List A that do not  exist in List b using LINQ

hi

I have 2 lists named ILIST<A> lstOne and ILIST<B> lstTwo. List "lstOne" has primpary field named "Id" and that is used as Foreign key in List "lstTwo" with name "OneId".how can i fetch the records from list lstOne that do not exist in lstTwo.actually i need the LINQ query of this sql stsmt below

Select * from tblA a where a.Id not in (Select OneId from tblB)

Thanks,
Usman

Answer : Fetch records from List A that do not  exist in List b using LINQ

Hi imrancs;

Yes if you want to get all List<A> objects that are not in List<B> objects then negate the where clause in the first post see my code below.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
IList<A> lstOne = new List<A>();
IList<B> lstTwo = new List<B>();

private void button1_Click(object sender, EventArgs e)
{
    IList<A> query = (from a in lstOne
                      where !lstTwo.Any(lst2 => lst2.OneID == a.ID)
                      select a).ToList();
}
Random Solutions  
 
programming4us programming4us