2013-04-15 201 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;