开发者

LINQ 2 OBJECTS - how to select distinct ones?

开发者 https://www.devze.com 2023-03-23 11:14 出处:网络
I have a collection of objects which are: someDate someString I need to select objects that are different by this two fields.

I have a collection of objects which are: someDate someString

I need to select objects that are different by this two fields. And I can not select it as objects in collections - I need to create new ones.

Say:

01/01/2011 "One"

01/01/2011 "One"

01/01/2011 "One"

01/01/2011 "Two"

(I need to note - this four are different to each other)

And I开发者_Python百科 need to get:

01/01/2011 "One"

01/01/2011 "Two"

How can I achieve it?

Thanks.


Your question is fairly unclear, but it sounds like you either just need to use Distinct after a projection:

var distinctDatesAndNames = items.Select(x => new { x.Date, x.Name })
                                 .Distinct();

or you need to use something like DistinctBy from MoreLINQ:

var distinctItems = items.DistinctBy(x => new { x.Date, x.Name });

It would really help if you could clarify your question though.

0

精彩评论

暂无评论...
验证码 换一张
取 消