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

本文关键字:子句 where Entities To 连接 Linq | 更新日期: 2023-09-27 17:56:03

我正在尝试在加入 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                           
          };

我想过滤文件集合,以便只有名称为"Test"的文件与 l1 连接。

我尝试过用l2.Name == "Test"过滤 l2,但它不起作用。它生成一个奇怪的查询,其中包含内部连接和左连接。

我该怎么做?

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

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

我认为这会起作用,但这每次都会查询(针对每条记录):

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