2015-09-24 20 views
0

我創建了一個message類爲我的用戶:進行分組的的BindingList

public class Message 
{ 
    [DisplayName("Message")] 
    public string shortContent { get; set; } 

    [DisplayName("Line")] 
    public int line { get; set; } 
    [DisplayName("File Name")] 
    public string file { get; set; } 
    [DisplayName("Level")] 
    public MessageLevel level { get; set; } 
    ... 
} 

public enum MessageLevel 
{ 
    Information, 
    Warning, 
    Error 
} 

要顯示這對我愛的用戶,我把它綁定到一個自定義綁定列表(爲了讓他們的消息進行排序)。

我希望能夠通過一些屬性(level,shortContent本質上)將消息分組,但我找不到任何簡單的方法與Linq

我試過用GroupBy沒有運氣。我很想在於如何將這些:

Message      Line File Name   Level Date    Read Full message 
The device is not ready. 46  OpenFileControl.cs Error 24/09/2015 3:01  True  
The device is not ready. 46  OpenFileControl.cs Error 24/09/2015 3:02  True  
Some Random message.  -1  Unkown    Error 

到:

Message      Count 
The device is not ready. 2 
Some Random message.  1 
+0

使用'GroupBy'時什麼不適合你? –

回答

1

使用的GroupBy應該工作。請記住,你是而不是修改你的綁定列表,所以任何綁定的東西都不會注意到你正在分組。

list.GroupBy(x => x.Message) 
    .Select(x => new { Message = x.Key, Count = x.Count() }) 
+0

我有:無法從''System.Collections.Generic.IEnumerable '轉換爲'System.Collections.Generic.IEnumerable ' 你有魔術嗎? –

+0

那麼,那是因爲要得到你需要將它投影到某個東西的計數,你不能將它分配回'Common.Message'的集合。你可以創建自己的類型'MessageCounts',它具有'Message'和'Count'屬性,並在'Select'子句中使用它。 你需要在某處顯示這個地方嗎?你想對分組數據做什麼? –

+0

沒關係,它的作品!謝謝 –