Linq To Entities,在左连接之前使用表上的where子句

时间:2013-01-24 12:57:42

标签: c# linq entity-framework linq-to-entities

在尝试加入Linq to Entities之前,我试图过滤我的数据集,但我找不到这样做的方法。我当前的linq查询是左联接是:

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                           
          };

我想过滤Files集合,只有名为“Test”的文件才与l1连接。

我尝试使用l2.Name == "Test"对l2进行过滤,但它无效。它使用内部联接和左联接生成一个奇怪的查询。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

我认为这会有效,但每次都会查询(对于每条记录):

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

最好在选择之前使用

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