Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get data from a list with a where clause to another list?

I have a list with multiple class that contain a Property that is an Integer (Id).

I have a List of Integer too.

Now, I would like to trim the List of my object to only those class that has the Property in the list of the integer.

Example:

List of MyObject
[MyObjectA].Id = 1
[MyObjectB].Id = 2
[MyObjectC].Id = 3
[MyObjectD].Id = 4

List of Integer
1
2

Final list should be 
[MyObjectA]
[MyObjectB]

How can I do it?

like image 587
Patrick Desjardins Avatar asked Nov 17 '25 13:11

Patrick Desjardins


1 Answers

You could use contains:

var finalList = originalList.Where(x => idList.Contains(x.Id)).ToList();

Or a join:

var finalList = (from entry in originalList
                join id in idList on entry.Id equals id
                select entry).ToList();
like image 67
Jon Skeet Avatar answered Nov 19 '25 02:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!