I’ve got a huge problem. I can’t figure out what’s wrong with my LINQ query. Here is SQL query that gets the job done:
SELECT TOP (100) PERCENT MIN(DateTime) AS DateTime, ETAG
FROM dbo.Table
GROUP BY ETAG, LEFT(DateTime, 10)
ORDER BY ETAG
This query gives me about 50k results, when executed. I want the same in LINQ. Here is what I tried:
from d in Table
group d by new { d.ETAG, Date = d.DateTime.Value.Year + d.DateTime.Value.Month + d.DateTime.Value.Day } into g
orderby g.Key.ETAG
select g.OrderBy(e => e.DateTime).FirstOrDefault()
When I execute this, it takes few min to be done, and gives me about 9k results. Something is wrong, but I can’t figure what. Please, help me get this right…
Thanks in advance
Using .NET 4 or higher, Entity Framework provides several utility functions specifically for date values. This may help you get over the hurdle with the first example and prevent you from having to eager load all rows before you group.
Edit:
Alternatively, if your aim is to group by just the date portion of the field you could try:
Reference: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/cb58ae5f-5db9-4054-92b6-a1bf63764574/