I have a list of objects that I want to transform into another list of objects.
The first object looks like this:
private class ObjectOne
{
public string Name { get; set; }
public string Item{ get; set; }
}
And another like this
public class ObjectTwo
{
public string Name { get; set; }
public IEnumerable<string> Items{ get; set; }
}
I would like to write a linq query or lambda expression that can convert a list of ObjectOne data into a list of ObjectTwo data. Each item in the list of ObjectTwo will contain a distinct name from ObjectOne along will an enumeration of all the items associated with that name.
For example the following list of ObjectOne data
"Name One", "Item One"
"Name One", "Item Two"
"Name Two", "Item Three"
would produce the following ObjectTwo list:
"Name One", {"Item One", "Item Two"}
"Name Two", {"Item Three"}
Use the GroupBy Linq operator. Then for each grouping, select the Key (the one you used to group it with), and convert the items of each group into a List.