如何根据字段值进行分组

时间:2015-12-30 09:49:51

标签: linq asp.net-mvc-4

我在下面的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创建文件夹。我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果你只需要为每个不同的 selecttionid 创建一个文件夹,那么你只需要使用Select with Distinct,就像那样:

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
}