在我的數據庫中有確實有國籍和分數的球員對象。在ASP.net中使用組合鍵的拼合LINQ查詢嵌套分組
我需要嵌套我的分組的可能性。因爲它們是由客戶給出的,所以在這裏看起來沒有選擇。 所以我要窩他們喜歡
Players.Where(p => p.Created <= /*some date*/)
.GroupBy(p => p.Nationality) // group them by their nationality
.Select(arg => new {
arg.Key,
Elements = arg.GroupBy(p => p.Points > 0) // group them by the ones with points and the ones without
})
. // here i need to flatten them by also combining the key(s) of the groupings to put them into a dictionary
.ToDictionary(/*...*/);
在年底,Dictionary
應該包含密鑰爲string
像["USA|true"]
,["USA|false"]
或["GER|true"]
各自的元素。
我猜SelectMany
是關鍵,但我沒有從哪裏開始實現這一點。
正如問題說明 - 分組匿名是不是一種選擇。 group-by子句由用戶(客戶端)給定的值動態構建,不能轉換爲匿名。例如客戶端發送'Player(Nation,HasPoints)'並將其轉換爲組 – KingKerosin
@TimSchmelter這就是'GroupBy'的構建方式。 http://stackoverflow.com/questions/40420025/passing-dynamically-build-expression-to-groupby-not-working。因爲它是從給定的字符串'new {/*...*/}動態構建的,ToString()'在這裏不起作用 – KingKerosin
@KingKerosin,那麼更新的答案呢? –