2011-02-07 38 views
1

有許多看似相關的Linq問題,但是快速瀏覽內容似乎並不足以回答我的問題,足以讓我有限的智能掌握。使用Linq將列表拆分成集合

我們有一個名爲PropertyInteractions的表,其中存儲了通過線程ID的線程消息,線程標識符是初始交互記錄的Id。鑑於以下Linq查詢(它檢索與用戶有關的所有交互),我怎樣才能將interactions分爲PropertyInteraction列表,按Id分組?

Dim interactions = (From interaction In propertyItem.PropertyInteractions _ 
        Where (interaction.SenderId = CurrentUser.ID OrElse _ 
          interaction.RecipientId = CurrentUser.ID) AndAlso _ 
          interaction.InteractionType = InteractionType.ViewRequest _ 
        Order By interaction.ThreadId _ 
        Select interaction) 

編輯:

由於喬恩的投入,這是我的時刻來到,儘管它可能會有所變動...

Dim interactions = _ 
    (From interaction In propertyItem.PropertyInteractions _ 
    Where (interaction.SenderId = CurrentUser.ID OrElse _ 
      interaction.RecipientId = CurrentUser.ID) AndAlso _ 
      interaction.InteractionType = InteractionType.ViewRequest _ 
    Order By interaction.ThreadId _ 
    Group interaction By interaction.ThreadId Into Group) 
+0

您的訂購現在有點怪......你爲什麼要下令分組查看'interaction.SendDate`命令的結果。那不計算... :) – 2011-02-07 12:21:01

+0

剛纔已經很明顯。 ;)我試圖嘗試的順序已經變得無關緊要了,所以我可以對分組感到厭煩。 – 2011-02-07 12:31:49

回答

4

聽起來像是你只是想:

Group interaction By interaction.Id Into Group 

查看docs for the Group By clause瞭解更多信息。 (它看起來像它的工作原理巧妙地不同於類似C#查詢表達式語法; VB專家可能是周圍提供更多的建議。)

+0

完美,喬恩 - 謝謝! – 2011-02-07 12:03:34

4
Dim groupedInteractions = interactions.GroupBy(Function(i) i.Id)