emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

Linq To Entities, Using where clause on table before left join

I am trying to filter my dataset before I do join in Linq to Entities but I could not find the way to do it. My current linq query is that makes left joins is :

from m in Products
          join f in FileFolders on m.ProductCode equals f.Name into list1
          from l1 in list1.DefaultIfEmpty()
          join p in Files on l1.FileFolderID equals p.FileFolderID into list2
          // I want something like p.Name == "Test" here
          from l2 in list2.DefaultIfEmpty()                                                     
          join b in BaseReferenceFile on l2.BaseReferenceFileID equals b.BaseReferenceFileID into list3
          from l3 in list3.DefaultIfEmpty()
          select new
          {
              //select some stuff here                           
          };

I want to filter Files collection such that only the files with name "Test" are joined with l1.

I have tried filtering on l2 with l2.Name == "Test" but it is not working. It generates a weird query with an inner join and a left join.

How can I do that?

Upvotes: 2

Views: 4494

Answers (2)

Nowshath
Nowshath

Reputation: 842

I think this will work but this will query each time (for each record):

   join p in (from f in Files where f.Name == "Test") on l1.FileFolderID equals p.FileFolderID into list2

its best to use where before select.

          ...from l3 in list3.DefaultIfEmpty()
          where (l1 !=null ? l1.Name == "Test" : true)
          select new
          {
              //select some stuff here                           
          };

Upvotes: 1

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

join p in Files.Where(m => m.Name == "Test") on l1.FileFolderID equals p.FileFolderID into list2

Upvotes: 3

Related Questions