Can you create a simple 'EqualityComparer' using a lambda expression

前端 未结 8 1466
半阙折子戏
半阙折子戏 2020-12-13 18:20

Short question:

Is there a simple way in LINQ to objects to get a distinct list of objects from a list based on a key property on the objects.

8条回答
  •  轮回少年
    2020-12-13 18:48

    This is the best i can come up with for the problem in hand. Still curious whether theres a nice way to create a EqualityComparer on the fly though.

    Galleries.SelectMany(x => x.Images).ToLookup(x => x.id).Select(x => x.First());
    

    Create lookup table and take 'top' from each one

    Note: this is the same as @charlie suggested but using ILookup - which i think is what a group must be anyway.

提交回复
热议问题