【问题标题】:Linq To Entities, Using where clause on table before left joinLinq To Entities,在左连接之前在表上使用 where 子句
【发布时间】:2013-01-24 12:57:42
【问题描述】:

我正在尝试在加入 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,但它不起作用。它会生成一个带有内连接和左连接的奇怪查询。

我该怎么做?

【问题讨论】:

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


    【解决方案1】:
    join p in Files.Where(m => m.Name == "Test") on l1.FileFolderID equals p.FileFolderID into list2
    

    【讨论】:

    • 谢谢,我试一试。是否可以使用Linq查询语法来做到这一点?
    • @emrenevayeshirazi 嗯,我必须承认我不知道,我不习惯那种语法。顺便说一句,您的查询中已经有一个DefaultIfEmpty() ;)
    • :) 我很好,我只是好奇。谢谢它正在工作。
    【解决方案2】:

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

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

    最好在选择之前使用 where

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多