Reputation: 16690
I have the following two objects:
ProjectPrimitives:
Name
ProjectId
StartDate
ProjectDetails
ProjectPrimitives
Description
Location
// Other details
In my application I have a List<ProjectDetails>
and would like to parse all of the primitives from them to get a List<ProjectPrimitives>
using a lambda expression. Effectively, I would like to replace this:
var primList = new List<ProjectPrimitives>();
foreach(ProjectDetails pd in myDetailsList)
{
primList.add(pd.primitives);
}
I have tried a few things but cannot find the right syntax, if this is in fact possible using lambda statements. One thing I've tried is:
var prims = myList.Where(i => i.primitives).ToList();
But the compiler is expecting a boolean inside the Where()
function and I don't know what else to put there.
Upvotes: 2
Views: 5846
Reputation: 16609
If ProjectDetails.primitives
is a single ProjectPrimitives
, you need Select()
to transform the list from ProjectDetails
to ProjectPrimitives
:
var prims = myList.Select(i => i.primitives).ToList();
If it is a collection, such as List<ProjectPrimitives>
, you need SelectMany()
:
var prims = myList.SelectMany(i => i.primitives).ToList();
Upvotes: 8