Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

return items that matches the list in linq

I have a list of objects with this properties:

name    “James" 
Country "Austrálie" 
Age 32

I have another list of object called person that contains names “james”, “Max”…….

So let’s say I have one list called MyListOfObjects that contains name,Country,Age and a list of objects called person that contains name property.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to return in Linq all the objects Name and country that match the other list names in person object .

so I was trying to do something like
var bb = MyListOfObjects.Where(b => persons.All(a => b.Name.Equals(a.Name))).ToList();

brings 0 results

while if I try
var desiredObjects = b.Table.Rows.Where(x => persons.Contains(x.Code)).ToList();
gives error as it can t convert

    private static IEnumerable<Person> persons = new[]
    {Name = “James”,Name = “Jimmy"}

Any help?

>Solution :

Use .Where() extenstion of Linq to filter through your objects list.Its similar to what you have already tried.

var desiredObjects = MyListOfObjects.Where(x => names.Contains(x.Name)).ToList();

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading