[英]Group by entities using LINQ
I have entities like this 我有这样的实体
class Test
{
string Name;
IList<SubTest> subTest;
}
class SubTest
{
string Id;
string value;
}
from a server I will get result of IEnumerable<Test>
. 从服务器上,我将得到IEnumerable<Test>
。 I want ot convert this result to SelectListItem so that I can bind this to mvc dropdown list. 我想将此结果转换为SelectListItem,以便将其绑定到mvc下拉列表。
Something like this 像这样
new SelectListItem(){Group=Test.Name, Value=SubTest.Id, Text= subTest.Value}
How can I get this result using linq? 如何使用linq获得此结果?
Use Select()
and SelectMany()
to transform the data into SelectListItem
使用Select()
和SelectMany()
将数据转换为SelectListItem
System.Collections.Generic.IEnumerable<Test> lResultFromServer = ...;
var lSelectListItems = lResultFromServer.SelectMany(s =>
s.subTest.Select(st => new System.Web.Mvc.SelectListItem {
Group = new System.Web.Mvc.SelectListGroup {Name = s.Name},
Value = st.Id,
Text = st.value
})
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.