Why is there no Linq method to return distinct values by a predicate?

后端 未结 4 1118
一整个雨季
一整个雨季 2020-12-04 16:53

I want to get the distinct values in a list, but not by the standard equality comparison.

What I want to do is something like this:

return myList.Dis         


        
4条回答
  •  无人及你
    2020-12-04 17:11

    But that seems messy.

    It's not messy, it's correct.

    • If you want Distinct Programmers by FirstName and there are four Amy's, which one do you want?
    • If you Group programmers By FirstName and take the First one, then it is clear what you want to do in the case of four Amy's.

    I can only use it here because I have a single key.

    You can do a multiple key "distinct" with the same pattern:

    return myList
      .GroupBy( x => new { x.Url, x.Age } )
      .Select( g => g.First() );
    

提交回复
热议问题