仅选择包含int数组中任何元素的项目

时间:2017-03-28 13:04:34

标签: c# linq extension-methods

假设我有一个班级:

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

以下代码:

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

现在我只需要选择包含ids int数组中任何元素的项目。我该怎么办?

1 个答案:

答案 0 :(得分:0)

一个简单的var filteredItems = items.Where(item => item.ClientIds.Any(ids.Contains)).ToList(); 应该这样做:

public static IEnumerable<Item> FilterItems(this IEnumerable<Item> source, IEnumerable<int> filter)
{
    return source.Where(item => item.ClientIds.Any(filter.Contains));
}

我不认为这里需要扩展方法,但你当然可以封装它:

Offending DSA key in ~/.ssh/known_hosts:5
  remove with:
  ssh-keygen -f "~/.ssh/known_hosts" -R [localhost]:8101
DSA host key for [localhost]:8101 has changed and you have requested strict checking.
Host key verification failed.