LINQ 2 OBJECTS - how to select distinct ones?
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.
精彩评论