GroupBy 绑定列表

GroupBy a bindingList

我为我的用户创建了 message class:

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
}

为了向我喜爱的用户显示它,我将它绑定到自定义绑定列表(以便他们对消息进行排序)。

我希望能够按某些属性(levelshortContent 本质上)对消息进行分组,但我找不到任何简单的方法来使用 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

使用 GroupBy 应该可以。请记住,您 不是 修改您的绑定列表,因此绑定到它的任何东西都不会注意到您正在分组。

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