I have a list of objects containing project name and owners of the project something like this:
[
{
project_name:"abc",
project_duration:"2 weeks",
contactInfo: [
{
name:"john"
},
{
name:"ron"
},
]
},
{
project_name:"def",
project_duration:"4 weeks",
contactInfo: [{
name:"steve"
}]
},
{
project_name:"xyz",
project_duration:"9 weeks",
contactInfo: [{
name:"john"
}]
}
]
How to create a list containing projects for each user from the above list?
[
{
name: "john",
projects:[
{
project_name:"abc"
},
{
project_name:"xyz"
}
]
}
]
Group your object based on the property Name
value.
Then Select the new object:
- The Name
is the grouping key.
- The sublist is just a projection from the old object.
data.GroupBy(x => x.contactInfo.name)
.Select(g =>
new Result
{
name = g.Key,
projects =
g.Select(x => new Project_t2
{
project_name = x.project_name,
projec_name = x.project_duration
}).ToList()
})
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.