I have two lists. I want to filter out the first list using an element inside the second list. So I have this code:
List<data> dataList = somedata;
IEnumerable<Filter> filterList = someFilterData;
and then I do the filtering using this code:
foreach (var s in filterList)
{
dataList = dataList .Where(l => l.dataId!= s.Id).ToList();
}
Can someone please suggest if this is a good enough approach or how we can make it better using some other technique. Note : The list might get large so we are also thinking about performance.
What you need is to take only these items which cannot be found in the filter list. You can do it in the "old school" way, using loops:
foreach (var listItem in dataList)
{
foreach (var filterItem in filterList)
{
if (listItem == filterItem)
{
dataList.Remove(listItem);
continue;
}
}
}
Or you can use LINQ to do the filtering:
dataList.Where(d => filterList.All(f => f.Id != d.dataId))
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