If there isn't a reason to group it, you could just switch your query up a little.
Up vote 4 down vote favorite share g+ share fb share tw.
I have a simple Linq to Enities table to query and get the most recent records using Date field So I tried this code: IQueryable alerts = GetAlerts(); IQueryable latestAlerts = from a in alerts group a by a. UpdateDateTime into g select g. OrderBy(a => a.
Identifier).First(); Error: NotSupportedException: The method 'GroupBy' is not supported. Is there any other way to get do it? Thanks a lot!
C# linq linq-to-entities link|improve this question edited May 3 '10 at 20:49BlueRaja - Danny Pflughoeft17.9k33775 asked May 3 '10 at 20:46Begemotya364.
Grouping by a DateTime doesn't make sense. If the resolution was, say, milliseconds you'd be unlikely to have a group with more than one item in it. Or is this really a Date field?
– Hightechrider May 3 '10 at 21:20.
If there isn't a reason to group it, you could just switch your query up a little: IQueryable alerts = GetAlerts(); IQueryable latestAlerts = alerts. OrderByDescending(a => a. UpdateDateTime).
This will load the entire database into memory when queried... – BlueRaja - Danny Pflughoeft May 3 '10 at 21:09 @BlueRaja - actually it won't do anything. Until you do something with latestAlerts like .First() or . Take(10) no database hit will occur.
– Hightechrider May 3 '10 at 21:18 @hightechrider: Hence the "when queried"... Presumably, the poster was looking for a complete answer. – BlueRaja - Danny Pflughoeft May 3 '10 at 21:19.
I've had a similair need. I want to get the typed record back, not a new anonymous object. To do that .First() can help.
Var query = from alert in m_alerts group alert by alert. Identifier into a select a. OrderByDescending(g => g.
UpdateDateTime).First(); Use the OrderByDescending to order them and take the top one with First(). You've grouped them by your identifier so you should only get the newest record for each identifier.
It should be IQueryable latestAlerts = (from a in alerts group a by a. UpdateDateTime into g order by g. Key select g).First(); GroupBy is definitely supported by Linq-to-Entities.
It's not working, the "order by g. Key" is "red" -> "a query body must end with select clause or a group clause – Begemotya May 4 '10 at 12:54 Any way I need to group by 2 fields "UpdateDateTime" and "Id" in order to get the most recent record for each id – Begemotya May 4 '10 at 12:57.
So the answer is: var query = from alert in m_alerts group alert by alert. Identifier into g select new { GroupIdentifier = g. Key, UpdateDateTime = g.
Max(p => p. UpdateDateTime) }; This will return the most recent records.
You know how to return also the column that had the most recent value in the same query... for instance comment (last comments). This is your code: var query = from alert in m_alerts group alert by alert. Identifier into g select new { GroupIdentifier = g.
Key, UpdateDateTime = g. Max(p => p. UpdateDateTime) }; for alert class: that contains {Identifier, UpdateDateTime}, I added 'comments' field.
So how include the most recent comments in my result list?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.