2015-12-30 84 views
1

我喜欢下面MVC执行LINQ查询后,结果分组:如何根据字段值

[0] = { albumid = 176, selecttionid = 243, orderid = 57 } 
[1] = { albumid = 177, selecttionid = 243, orderid = 57 } 
[2] = { albumid = 178, selecttionid = 243, orderid = 57 } 

[3] = { albumid = 19, selecttionid = 321, orderid = 137 } 

......

但我需要为每个不同的selecttionid创建的文件夹。我怎样才能做到这一点?

+3

使用'GroupBy'方法。 –

回答

0

如果你只需要创建一个文件夹的每个diferente selecttionid,比你只需要使用选择具有层次分明,这样的:

var selections = mylist.Select(x => x.selecttionid).Distinct(); 
foreach(var selection in selections) 
{ 
    //Code that create a folder for the selectionId 
} 

如果您从列表中需要的值,比你能使用GroupBy。

var groupedSelections = mylist.GroupBy(x => x.selecttionid); 
foreach(var groupSelecion in groupedSelections) 
{ 
    //Code that create a folder for the groupSelecion.Key 
}