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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With