How to load just the last record from entity with LINQ?
Asked Answered
W

5

20

I want to fetch value of field named "Gram" from the last record and put its value into a variable, without using any conditions.

First I tried

int value = int.Parse(Entity.TblGold.LastOrDefault().Gram.ToString());

Second I tried

int value = int.Parse(Entity.TblGold.Select(p => p.Gram).Last().ToString());

I just receive this exception:

LINQ to Entities does not recognize the method 'DataModel.TblGold LastOrDefault[TblGold](System.Linq.IQueryable``1[DataModel.TblGold])' method, and this method cannot be translated into a store expression.

Wellinformed answered 1/10, 2013 at 16:45 Comment(3)
Side note: there is generally no concept of "last" record in databases... Only "last, when sorted by field(s)".Tommi
There Should be a solution.I Have my table in my Entity.for example 200 records...and i want a value of special field just from LAST record.there is no condition.so how can i grab this value from field????? :(Wellinformed
Can you suggest me Code Please, i'm really sorry to disturb you timeWellinformed
F
45

Last or LastOrDefault are not supported in LINQ to Entities. You can either iterate your query using ToList or ToArray and then apply Last or you can order by descending and then use the First like:

int value = int.Parse(Entity.TblGold
                            .OrderByDescending(p => p.Gram)
                            .Select(r => r.Gram)
                            .First().ToString());
Furfuraceous answered 1/10, 2013 at 16:52 Comment(5)
@amin, you are welcome and I see you joined today, Welcome to stackoverflow.Furfuraceous
It's kind of you :) Thank you very much. So as i figure it out,we should have some kind of instance to do last or first or like these?Wellinformed
@amin, not really, for First, it gets translated into SELECT Top (1) So you don't really have to iterate the query. The above query in answer is getting translated into something like SELECT TOP 1 Gram from tblGold Order by Gram DESC; . With Last/LastOrDefault the expression can't translate into under laying data source language (SQL).Furfuraceous
@amin, sorry I missed the part for selecting a particular field. Just check the updated answerFurfuraceous
It really helped me out :)Wrapping
R
1

If you have an incremental id:

int idx = tb.Max(i => i.Id);
var row = tb.FirstOrDefault(i => i.Id == idx);
Renovate answered 23/11, 2021 at 14:31 Comment(0)
E
0

You can't do it in one query, but you can do it in two.

var countOfRows = tbl.Count();

var lastRow = tbl.Skip(countOfRows - 1).FirstOrDefault();
Endodontics answered 1/10, 2013 at 16:54 Comment(1)
your code doesn't compile because off an error for nothing stored or use orderbyWellinformed
F
0

It is your answer and you don't need to convert.

int value = Entity.TblGold.OrderByDescending(p => p.Id).First().Gram;
Forfar answered 25/10, 2022 at 12:35 Comment(0)
K
-1

You can use order by 1 == 1 and it works

var countOfRows = tbl.Count();    
var lastRow = tbl.OrderBy(c => 1 == 1).Skip(countOfRows - 1).FirstOrDefault();
Knapp answered 28/10, 2015 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.