0

I have list and I need to search for specific item and get related data from this list.

 public People(string name, string talent)
        {
            Name= name;
            Talent= talent;
        }
        public string Name;
        public int Talent;

I added many items to that list

List<People> peopleList = new List<People>();
For example;
"John","magic",
"John","windpower",
"John","electricpower",
"John","firepower",
"Luigi","storm",
"Luigi","fire",

I'm trying to get people all talents like;

string talentsofJohn = "magic,windpower,electricpower,firepower";
string talentsofLuigi = "storm,fire";

I have tried many ways especially Linq but couldnt make it;

var talentsofJohn = peopleList.Find(item => item.name == "John");
SPYBUG96
  • 1,089
  • 5
  • 20
  • 38
David K.
  • 3
  • 1

5 Answers5

2

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;
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You need to use a combination of Where (to filter the list) and Select (to pick out an individual property) :

var talents = peopleList.Where(p => p.Name == "John").Select(p => p.Talent);

If you then want it in a comma separated string you can use string.Join on the returned enumeration

var asString = string.Join(",", talents)
Sean
  • 60,939
  • 11
  • 97
  • 136
0

Your Talent type is int, change it to string or it would not work as you want.

Then try this to retrive talents for specific person:

string userName = "John";
var talentsOfPerson = peopleList
                        .Where(person => person.Name == userName)
                        .Select(person => person.Talent)
                        .ToList();

Also the proper name for your class should be Person not People

Markiian Benovskyi
  • 2,137
  • 22
  • 29
0

First, let's use Where to filter your list down to just people named John.

var johnList = peopleList.Where(item => item.name == "John");

Next, let's use Select to only extract the people's talents:

var talentList = johnList.Select(item => item.talent);

Finally, you can use string.Join to convert an array or list to a string:

string talents = string.Join(",", talentList);

LINQ allows you to chain these together, so you can do it all in a single line:

string talents = string.Join(
    ",", 
    peopleList
        .Where(item => item.name == "John")
        .Select(item => item.talent)
    );
Chris
  • 3,328
  • 1
  • 32
  • 40
0

Use loop of your choice like it could be for loop,for each loop , while loop.. etc

foreach(people p in peoplelist)
{ 
  if(p.name=="john")
   {

    Console.WriteLine(p.name + " " + p.talent)

   }
}

OR

for(int i=0;i<peoplelist.Count;i++)
{
     if(peoplelist[i].name=="john")
       {

        Console.WriteLine(peoplelist[i].name + " " + peoplelist[i].talent)

       }
}

hope this will help you