2013-10-11 37 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行(S)。我們需要首先修復你的SQL,然後我們需要轉換爲Linq。 – Aron

+0

您可以查看我的編輯。你需要的是Group By'id',然後得到每個'id'具有最大'beloneMonth'的行。 –

回答

1

以下將beloneMonth

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

它的工作原理!感激。 – user2869974