0

Assume I have a class:

public class Item
{
    ...
    public IEnumerable<int> ClientsIds { get; set; }
}

And the following code:

List<Item> items = GetItems();
int[] ids = GetIds();

Now I need to select only such items that contain any element in ids int array. How can I do it?

René Vogt
  • 43,056
  • 14
  • 77
  • 99
Dmitry S
  • 33
  • 6

1 Answers1

0

Well a simple Where should do it:

var filteredItems = items.Where(item => 
                      item.ClientIds.Any(ids.Contains)).ToList();

I don't see a need for an extension method here, but you could of course encapsulate it:

public static IEnumerable<Item> FilterItems(this IEnumerable<Item> source, IEnumerable<int> filter)
{
    return source.Where(item => item.ClientIds.Any(filter.Contains));
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99