2013-10-11 6 views
1
select * 
from monthStatistics ms 
    where ms.beloneMonth = 
     (
      select max(ms2.beloneMonth) 
      from monthStatistics ms2 
      where ms.materialDictionaryId = ms2.materialDictionaryId 
      and ms.locationId = ms2.locationId 
      and ms.price=ms2.price 
     ) 

샘플 데이터Linq에 이것을 쓰는 방법? 그룹 얻을 최대

id beloneMonth  materialDictinaryId price acount 
1 2013/7   1      100 200 
2 2013/7   2      100 200 
3 2013/8   1      100 200 
4 2013/8   1      200 200 

결과 :

id beloneMonth  materialDictinaryId price acount 
2 2013/7   2      100 200 
3 2013/8   1      100 200 
4 2013/8   1      200 200 

그룹은 최대 월 행을 얻을.

+0

당신은 정말 당신이 뭘 하려는지 표시되지 않습니다 준 SQL의 max 값을 가지고 monthStatistics의 행을 반환합니다. 먼저 SQL을 수정 한 다음 Linq로 변환해야합니다. – Aron

+0

내 편집을 확인할 수 있습니다. 필요한 것은'id'에 의해 그룹화 된 다음 각 id에 대해 최대'beloneMonth' 행을 가져옵니다. –

답변

1

다음은 beloneMonth

monthStatistics 
    .GroupBy(x=>x.id) 
    .SelectMany 
    (
     x=> 
     x.Where 
     (
      z=> 
      z.beloneMonth == x.Max(y=>y.beloneMonth) 
     ) 
    ); 
+0

작동합니다! 기분 좋은. – user2869974