In this query:
Apr 01, 2019 I ended up with caching it by converting collection to List and then calling sequenceList.RemoveAt(sequence.Count - 1).In my case it is acceptable because after all LINQ manipulations I have to convert it to array or IReadOnlyCollection anyway. I wonder what is your use case where you do not even consider caching? Nov 13, 2011 - example: Enumerable.Range(1, 10).TakeLast(3) //.
I had to switch it to this for it to work
I couldn't even use
p.First()
, to mirror the first query.Why are there such basic limitations in what's otherwise such a robust ORM system?
Mong Zhu16.7k77 gold badges2727 silver badges5353 bronze badges
bevacquabevacqua28.5k4141 gold badges144144 silver badges270270 bronze badges
6 Answers
That limitation comes down to the fact that eventually it has to translate that query to SQL and SQL has a
SELECT TOP
(in T-SQL) but not a SELECT BOTTOM
(no such thing).There is an easy way around it though, just order descending and then do a
First()
, which is what you did.EDIT:Other providers will possibly have different implementations of
SELECT TOP 1
, on Oracle it would probably be something more like WHERE ROWNUM = 1
EDIT:
Another less efficient alternative - I DO NOT recommend this! - is to call
.ToList()
on your data before .Last()
, which will immediately execute the LINQ To Entities Expression that has been built up to that point, and then your .Last() will work, because at that point the .Last()
is effectively executed in the context of a LINQ to Objects Expression instead. (And as you pointed out, it could bring back thousands of records and waste loads of CPU materialising objects that will never get used)Again, I would not recommend doing this second, but it does help illustrate the difference between where and when the LINQ expression is executed.
Neil FenwickNeil Fenwick5,21233 gold badges2727 silver badges3535 bronze badges
AzarsaAzarsa94322 gold badges2424 silver badges3131 bronze badges
Replace
Last()
by a Linq selector OrderByDescending(x => x.ID).Take(1).Single()
Something like that would be works if you prefert do it in Linq :
Ema.HEma.H1,98733 gold badges2020 silver badges3535 bronze badges
Yet another way get last element without OrderByDescending and load all entities:
Stas BoyarincevStas Boyarincev
That's because LINQ to Entities (and databases in general) does not support all the LINQ methods (see here for details: http://msdn.microsoft.com/en-us/library/bb738550.aspx)
What you need here is to order your data in such a way that the 'last' record becomes 'first' and then you can use FirstOrDefault. Note that databasese usually don't have such concepts as 'first' and 'last', it's not like the most recently inserted record will be 'last' in the table.
This method can solve your problem
KazemKazem
Adding a single function
Example:
AsEnumerable()
before Select function worked for me.Example:
Ref:https://www.codeproject.com/Questions/1005274/LINQ-to-Entities-does-not-recognize-the-method-Sys
Artem LevitinArtem Levitin