I have a List<int>
and a List<customObject>
. The customObject class has an ID property. How can I get a List<customObject>
containing only the objects where the ID property is in the List<int>
using LINQ?
Edit: I accepted Konrads answer because it is easier/more intuitive to read.
The next example filters a list with LINQ's Where method. var vals = new List<int> {-1, -3, 0, 1, 3, 2, 9, -4}; List<int> filtered = vals. Where(x => x > 0). ToList(); Console.
In this article Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition. It is also known as selection. The following illustration shows the results of filtering a sequence of characters.
var result = from o in objList where intList.Contains(o.ID) select o
using System.Linq;
objList.Where(x => intList.Contains(x.id));
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