You're almost there, you're just not thinking in "collection of items" mode
var talentsofJohn = peopleList.Find(item => item.name == "John");
Only gets you a single People
object because Find
stops as soon as the first match is found
You want to get a collection of all matching items, and you want to find by the item.Name == "John"
- note that I've changed the capitalisation of your name
to Name
because that's the name of the publicly visible property.
For this, you can use LINQ .Where
- the result of this Where is an enumerable collection of People
objects. From that, you want to LINQ Select
just the Talent
property from each person (the select will give you an enumerable collection of talent strings):
var talentsofJohn = peopleList.Where(p => p.Name == "John").Select(q => q.Talent);
If you want them all in a string, separated by commas:
var str = string.Join(",", talentsOfJohn);
Note, you can avoid the use of string.Join, and do it all in LINQ using the Aggregate method, but there are a few good reasons why you might not want to do so. For more info including arguments as to why, see Concat all strings inside a List<string> using LINQ
A final note.. List.Find
isn't LINQ.. It's just a plain old method on List
. As a result, if you do put this LINQ code into your app and it says something like:
List does not contain a definition for 'Where' and no extension method accepting a first argument of blahblah was found, are you missing an assembly reference?
You need to ensure your class file has at the top:
using System.Linq;