2012-11-13 58 views
0

我正在嘗試創建隊列類別顯示的隊列列表。每個隊列類別都被賦予一個Enum值。List by GroupBy from IEnumerable

public enum QueueCategory 
{ 
    None=0, 
    Critical=1, 
    High=2, 
    Orphaned=3, 
    Missing=4 
} 

而對於每個類別,我想顯示這些字段。

public class QueueInformation 
{ 
    public string Name { get; set; } 
    public Decimal PercentOfThreshold { get; set; } 
    public string Host { get; set; } 
    public DateTime OldestArrival { get; set; } 
    public QueueCategory Category { get; set; } 
} 

}

我怎麼會去讓QueueInformation由QueueCategory顯示這兩個網頁鏈接?

+3

當你的標題所說,使用'GroupBy' –

回答

2
IEnumerable<QueueInformation> infos = ...; 

foreach (var categoryGroup in infos.GroupBy(i => i.Category)) 
{ 
    Console.WriteLine("Current category: {0}", categoryGroup.Key); 

    foreach (var queueInfo in categoryGroup) 
    { 
    Console.WriteLine(queueInfo.Name /*...*/); 
    } 

    Console.WriteLine("=========================="); 
} 
0

我假設你想在QueueCategory下令來源:

IEnumerable<QueueInformation> list = new BindingList<QueueInformation>(); 
var orderedList = from l in list orderby l.Category select l; 

希望這有助於