Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a "join" in a LINQ query against the Entity Framework

I have the following table structure which has been imported into the Entity Framework. I need to write a LINQ query where I select the entities of Table1, where a field in Table2 is equal to true, and a field in Table 3 is equal to a specific GUID.

Could someone help with this?

Thanks you.

alt text http://digitalsamurai.us/images/drawing2.jpg

like image 634
Sako73 Avatar asked Dec 14 '25 12:12

Sako73


1 Answers

Try:

from t3 in dataContext.Table3
  where t3.Guidfield == someGuid
  from t2 in t3.Table2
  where t2.Field // boolean field is true
  select t2.Table1;

EDIT: As requested, equivalent lambda expression syntax:

dataContext.Table3.Where(t3 => t3.Guidfield == someGuid)
              .SelectMany(t3 => t3.Table2)
              .Where(t2 => t2.Field)
              .Select(t2.Table1);
like image 155
Håvard S Avatar answered Dec 16 '25 10:12

Håvard S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!