Double select in list
.net, c#, linq
Solution
Assuming you want all of the names in a list, you can do:
List<Name> matchingNames = Items.Where(a => a.id == 1).Select(a => a.names);
Or if you want a list of the string names from the list, you can do:
List<string> matchingNames = Items
.Where(a => a.id == 1)
.SelectMany(n => n.names)
.Select(n => n.name)
.ToList();
Then, if you're using my second statement, you can output a list in the format `item, item, item` by doing:
string outputtedNames = string.Join(", " + matchingNames);
EDIT: As requested in comments, here's how you can get names by ID based on the Name ID:
List<Name> matchingNames = Items
.SelectMany(a => a.names)
.Where(n => n.id == 1)
.ToList();
EDIT 2: To display name items and items that both have an ID of 1, try this:
List<Name> matchingNames = Items
.Where(a => a.id == 1)
.SelectMany(a => a.names)
.Where(n => n.id == 1)
.ToList();
Problem
How can i select name in list? which is itself in list??? My struct: ``` public class Item { int id; List<Name> names; } public class Name { int id; string name; } List<Item> Items; ``` code: ``` Items.Select(a => a.id = 1) //whats next ```