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.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
amin
  • 1,194
  • 1
  • 12
  • 20
  • 2
    Side note: there is generally no concept of "last" record in databases... Only "last, when sorted by field(s)". – Alexei Levenkov Oct 01 '13 at 16:50
  • 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????? :( – amin Oct 01 '13 at 16:54
  • Can you suggest me Code Please, i'm really sorry to disturb you time – amin Oct 01 '13 at 16:55

5 Answers5

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());
Habib
  • 219,104
  • 29
  • 407
  • 436
  • @amin, you are welcome and I see you joined today, Welcome to stackoverflow. – Habib Oct 01 '13 at 16:57
  • 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? – amin Oct 01 '13 at 17:01
  • @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). – Habib Oct 01 '13 at 17:06
  • @amin, sorry I missed the part for selecting a particular field. Just check the updated answer – Habib Oct 01 '13 at 17:53
  • It really helped me out :) – Moeez Oct 22 '17 at 08:21
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();
Garrison Neely
  • 3,238
  • 3
  • 27
  • 39
0

If you have an incremental id:

int idx = tb.Max(i => i.Id);
var row = tb.FirstOrDefault(i => i.Id == idx);
Tostone
  • 95
  • 1
  • 9
0

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

int value = Entity.TblGold.OrderByDescending(p => p.Id).First().Gram;
Saeid
  • 422
  • 4
  • 9
-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();
NASSER
  • 5,900
  • 7
  • 38
  • 57