Filter a list by another list C#

If you have a situation like:

List<ItemBO> items;
List<ItemCategoryBO> categories;

and you wish to get all the items that have a category that is in your list of categories, you can use this:

IEnumerable<ItemBO> result = items.Where(item =>
    categories.Any(category => category.ItemCategory.equals(item.ItemCategory))); 

The Any operator enumerates the source sequence and returns true if any element satisfies the test given by the predicate. In this case, it returns true if the categories list contains an ItemCategoryBO where its ItemCategory string is the same as the item’s ItemCategory string.
More information about it on MSDN

Leave a Comment