如何从实体LINQ加载刚刚过去的记录? [英] How to load just the last record from entity with LINQ?

查看:113
本文介绍了如何从实体LINQ加载刚刚过去的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取从最后一个记录命名为克字段值,并把它的值到一个变量,而无需使用任何条件。

首先,我试过

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

其次,我试过

  int值= int.Parse(Entity.TblGold.Select(P => p.Gram)。去年()的ToString());

我刚刚收到此异常:


  

LINQ到实体无​​法识别方法DataModel.TblGold LastOrDefault [TblGold](System.Linq.IQueryable``1 [DataModel.TblGold])的方法,而这种方法不能被翻译成商店前pression。



解决方案

最后 LastOrDefault 不支持LINQ到实体。您可以使用迭代查询了ToList 的ToArray ,然后申请最后,也可以通过降序排列,然后使用首先这样的:

  int值= int.Parse(Entity.TblGold
                            .OrderByDescending(P => p.Gram)
                            。选择(R = GT; r.Gram)
                            。。首先()的ToString());

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.

解决方案

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());

这篇关于如何从实体LINQ加载刚刚过去的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆