2013-04-15 159 views
-1

我有点困惑于将SQL查询转换为LINQ。任何人都可以帮我解决问题。 这里是我的查询提前SQL自加入查询到LINQ查询

SELECT x.* 
    FROM FilterType x 
    JOIN (SELECT t.FilterType 
      FROM FilterType t 
      where FilterId in (7,15) 
     GROUP BY t.FilterType 
     HAVING COUNT(t.FilterType) > 1) y ON y.FilterType = x.FilterType 

感谢。

+0

您可以使用LINQPad将SQL查询转换为LINQ。 –

+0

@JibranKhan我相信LINQPad会做相反的事 - 它显示为LINQ查询生成的SQL。为了生成LINQ,你需要像Linqer –

+0

然后,还有一个叫Linqer,但在这里做回顾答案http://stackoverflow.com/questions/12238423/linqpad-convert-sql-to-linq-command –

回答

1

假设您有int[] ids = { 7, 15 }。然后查询将看起来像:

from t in FilterType.Where(x => ids.Contains(x.FilterId)) 
group t by t.FilterType into g 
where g.Count() > 1 
from f in g 
select f 

或用方法的语法:

FilterType.Where(x => ids.Contains(x.FilterId)) 
      .GroupBy(t => t.FilterType) 
      .Where(g => g.Count() > 1) 
      .SelectMany(g => g); 

生成的SQL不会完全是你的,但结果应该是一样的。

0
from a in FilterType 
join b in 
    (
     from x in FilterType 
     where (new int[]{7, 15}).Contains(x.FilterID) 
     group x by new {x.FilterType} into g 
     where g.Count() > 1 
     select new {FilterType = g.Key.FilterType} 
    ) on a.FilterType equals b.FilterType 
select a;