I tried to find answer for my problem, but I couldn't. I have table with ID, WorkerID, Function(int), and DateTime. Each worker has multiple records for each day. I need to get only first record for each day, for all workers. This query gives the results I wanted:
SELECT WorkerID, MIN(DateTime) AS DateTime
FROM Records
GROUP BY WorkerID, LEFT(DateTime, 10)
ORDER BY WorkerID
But I want to select all columns (ID, WorkerID, Function, and DateTime). How can I do that? Also, I need this query for ASP.NET MVC3 application, so if anyone could write this for me in LINQ, and explain how to transform that query into IEnumerable(Record) or IQuerible(Record), I would be very thankful. Thanks in advance guys, hope you can save me.
Edit: Your LINQ query worked, but I had a lot of issues regarding perfomance, because of using a lot of Views, not Tables. Now I switched to Table, and I want do query directly on Table. Structure is the same, just instead of WorkerID, I have ETAG, which is the same. But I have problem now. SQL query mentioned above, gives me about 50k results on table, which is correct, but now your LINQ query gives me only 9k, so something is wrong. Here is query:
from d in Records
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()
Please, can you help me with this one...