【问题标题】:How can you handle an IN sub-query with LINQ to SQL?如何使用 LINQ to SQL 处理 IN 子查询?
【发布时间】:2010-09-08 06:10:37
【问题描述】:

我对此有点困惑。基本上我想在 LINQ to SQL 中执行以下 SQL 查询:

SELECT f.* 
FROM Foo f
WHERE f.FooId IN (
    SELECT fb.FooId
    FROM FooBar fb
    WHERE fb.BarId = 1000
)

如有任何帮助,我们将不胜感激。

【问题讨论】:

    标签: sql linq linq-to-sql


    【解决方案1】:

    尝试使用两个单独的步骤:

    // create a Dictionary / Set / Collection fids first
    var fids = (from fb in FooBar
                where fb.BarID = 1000
                select new { fooID = fb.FooID, barID = fb.BarID })
                .ToDictionary(x => x.fooID, x => x.barID);
    
    from f in Foo
    where fids.HasKey(f.FooId)
    select f
    

    【讨论】:

      【解决方案2】:
      from f in Foo
          where f.FooID ==
              (
                  FROM fb in FooBar
                  WHERE fb.BarID == 1000
                  select fb.FooID
      
              )
          select f;
      

      【讨论】:

        【解决方案3】:

        试试这个

        var fooids = from fb in foobar where fb.BarId=1000 select fb.fooID
        var ff = from f in foo where f.FooID = fooids select f
        

        【讨论】:

          【解决方案4】:

          看看this article。基本上,如果你想得到IN的等价物,你需要先构造一个内部查询,然后使用Contains()方法。这是我的翻译尝试:

          var innerQuery = from fb in FoorBar where fb.BarId = 1000 select fb.FooId;
          var result = from f in Foo where innerQuery.Contains(f.FooId) select f;

          【讨论】:

          • 感谢您的链接 - 这正是我所需要的。也感谢其他所有人的回答。
          • 使用第一个查询构造字典可能会有更好的性能,因为第二个查询中的 Contains() 调用可以在 O(1) 中完成,而不是 O(n)。
          • 达人,LINQ to SQL会转成SQL查询。迭代对象集合时,字典将很有用。
          • @Samuel_Jack 如何在 LinqDatasource 中使用它?
          • 链接现在已经失效,答案也没有多大用处。如果这个答案包含来自链接文章的更多信息,那就更好了,正是为了防止这种情况。
          【解决方案5】:

          在 LINQ to SQL 中实现 IN 的一般方法

          var q = from t1 in table1
                  let t2s = from t2 in table2
                            where <Conditions for table2>
                            select t2.KeyField
                  where t2s.Contains(t1.KeyField)
                  select t1;
          

          在 LINQ to SQL 中实现 EXISTS 的一般方法

          var q = from t1 in table1
                  let t2s = from t2 in table2
                            where <Conditions for table2>
                            select t2.KeyField
                  where t2s.Any(t1.KeyField)
                  select t1;
          

          【讨论】:

          • 快速且切中要害 - 很好的答案。即使在单独的语句中构建内部查询绝对清楚,也不妨知道如何内联。谢谢!
          • @aku 这种方法比@Samuel Jack 的方法更好吗?
          • 这比方法链语法更易读。谢谢。
          • 感谢咨询!!小观察:在 vb 中,t2s = 右侧的查询语句应该用括号括起来。我在vs2013
          【解决方案6】:
          var foos = Foo.Where<br>
          ( f => FooBar.Where(fb.BarId == 1000).Select(fb => fb.FooId).Contains(f.FooId));
          

          【讨论】:

            【解决方案7】:

            //首先创建一个字典/集合/集合fids

            Find Other Artilces

            var fids = (from fb in FooBar
                        where fb.BarID = 1000
                        select new { fooID = fb.FooID, barID = fb.BarID })
                        .ToDictionary(x => x.fooID, x => x.barID);
            
            from f in Foo
            where fids.HasKey(f.FooId)
            select f
            

            【讨论】:

              【解决方案8】:

              //首先创建一个字典/集合/集合fids

              Find Other Artilces

              var fids = (from fb in FooBar where fb.BarID = 1000 select new { fooID = fb.FooID, barID = fb.BarID }) .ToDictionary(x => x.fooID, x => x.barID);
              
              from f in Foo where fids.HasKey(f.FooId) select f
              

              【讨论】:

                【解决方案9】:
                from f in foo
                where f.FooID equals model.FooBar.SingleOrDefault(fBar => fBar.barID = 1000).FooID
                select new
                {
                f.Columns
                };
                

                【讨论】:

                  猜你喜欢
                  • 2010-09-25
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-07-23
                  • 2011-06-16
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多