Reputation: 9278
I have a class BaseEvent
and it have the following fields:
Name, RecurrenceStart, RecurrenceEnd, SimillarId
For example we have a data in a table:
Name RecurrenceStart RecurrenceEnd SimillarId
Event1 2012-02-21 09:00:00.000 2012-02-28 11:30:00.000 1
Event1 2012-02-21 04:30:00.000 2012-02-28 07:00:00.000 1
Event2 2012-02-21 06:30:00.000 2012-02-29 09:00:00.000 2
And we have the following query:
var today = DateTime.Now;
return Database.BaseEvents.Where(e => e.RecurrenceStart.Value.Date <= today
&& e.RecurrenceEnd.Value.Date >= today).ToList();
The query return all 3 records(for current date). But the first two record is the same event.
I want that in the result will only two records: 3 and (1 or 2).
How can I do this?
Thanks.
P.S. I was introduce SimillarId for this purpose, but I don't know how to build a query with it or not.
P.S.2. Change question name if it necessery.
Upvotes: 0
Views: 184
Reputation: 18290
Try this:
var today = DateTime.Now;
return Database.BaseEvents.Where(e => e.RecurrenceStart.Value.Date <= today
&& e.RecurrenceEnd.Value.Date >= today)
.GroupBy(v => v.Name).Select(g => g.First()).ToList();
Upvotes: 1
Reputation: 6888
Use .Distinct()
with an IEqualityComparer<BaseEvent>
as argument which you have to implement first.
Upvotes: 2