2013-04-15 5 views
-1

SQL 쿼리를 LINQ로 변환하는 데 조금 어려움이 있습니다. 어떤 몸이라도 도와주세요. 내 검색어입니다LINQ 쿼리에 대한 SQL 자체 조인 쿼리

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;