Intersect LINQ query

后端 未结 7 1965
北恋
北恋 2020-11-30 00:57

If I have an IEnumerable where ClassA exposes an ID property of type long. Is it possible to use a Linq query to get all instances of ClassA with ID belonging to a second IE

7条回答
  •  隐瞒了意图╮
    2020-11-30 01:26

    Naming things is important. Here is an extension method base on the Join operator:

    private static IEnumerable IntersectBy(
        this IEnumerable source,
        IEnumerable keys,
        Func keySelector)
            => source.Join(keys, keySelector, id => id, (o, id) => o);
    

    You can use it like this var result = items.IntersectBy(ids, item => item.id).

提交回复
热议问题