不支持 Last 和 LastOrDefault
我正在尝试获取列表中的第一个和最后一个值。支持查询运算符 First()
,但 Last()
和 LastOrDefault()
会出错。我是否错误地使用了 Last()
运算符?
var purchaseBills = db.PurchaseBills.OrderBy(p => p.BillID);
if (purchaseBills.Count() >0)
{
var firstBill = purchaseBills.First(); // This is supported
// Attempt 1
var lastBill = purchaseBills.Last(); // Not supported
// Attempt 2
var lastBill = purchaseBills.LastOrDefault(); // Not supported
//Attempt 3
var lastBill = purchaseBills.Reverse().First(); // Not supported
textBoxPurchaseBillFrom.Text = firstBill.BillNo.ToString();
textBoxPurchaseBillTo.Text = lastBill.BillNo.ToString();
}
更新:
--错误--
尝试 1:不支持查询运算符“
Last
”。尝试 2:不支持查询运算符“
LastOrDefault
”。尝试 3:不支持查询运算符“
Reverse
”。
I am trying to get the first and last values in a list. The query operator First()
is supported but Last()
and LastOrDefault()
give an error. Am I using the Last()
operator incorrectly?
var purchaseBills = db.PurchaseBills.OrderBy(p => p.BillID);
if (purchaseBills.Count() >0)
{
var firstBill = purchaseBills.First(); // This is supported
// Attempt 1
var lastBill = purchaseBills.Last(); // Not supported
// Attempt 2
var lastBill = purchaseBills.LastOrDefault(); // Not supported
//Attempt 3
var lastBill = purchaseBills.Reverse().First(); // Not supported
textBoxPurchaseBillFrom.Text = firstBill.BillNo.ToString();
textBoxPurchaseBillTo.Text = lastBill.BillNo.ToString();
}
Update:
--Errors--
Attempt 1: The query operator '
Last
' is not supported.Attempt 2: The query operator '
LastOrDefault
' is not supported.Attempt 3: The query operator '
Reverse
' is not supported.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
AsEnumerable()
,而不是通过调用ToList()
或ToArray()
将其放入自己的列表中。OrderByDescending()
,Count()
,我会使用Any()
。ToList()
orToArray()
i would prefer to useAsEnumerable()
.OrderByDescending()
Count()
i would useAny()
.要么将 OrderBy 切换为
(并首先使用),要么执行类似的操作(
如果这不是很昂贵)。
either you switch your OrderBy to
(and use first) or you do something like
if this is not to expensive.
后端数据库不支持
Last
。您应该尝试其他技术:使用
OrderByDescending
运行查询,以便您请求的项目排在前面。照常编码您的 LINQ 查询,但强制 Linq2Sql 将其呈现到 CLR 集合,然后您就可以自由访问本地的所有内容,包括
Last
。示例:Last
is not supported by the back-end DB. You should try other techniques:Run your query using
OrderByDescending
so your requested item comes first.Code your LINQ query as usual, but enforce Linq2Sql to render it to a CLR collection and then you'll have free access to everything locally, including
Last
. Example:问题是
Last
或Reverse
无法轻松转换为 SQL,因此要么将其转换为内存中的内容(ToList
、ToArray
) 如果记录不会太多,或者您可以第二次运行查询,使用OrderByDescending
而不是OrderBy
并使用 <代码>首先。The problem is that there's no easy translation into SQL for
Last
orReverse
, so either convert it to something in memory (ToList
,ToArray
) if there aren't going to be too many records, or you could run the query a 2nd time, withOrderByDescending
instead ofOrderBy
and useFirst
.我尝试不使用
LastOrDefault()
因为有时它不起作用或不支持。我按
desc
对id
进行排序,然后获取第一条记录。I try not to use
LastOrDefault()
because sometime it doesn't work or support.I'm sorting
id
bydesc
and then grab the first records.这与
Last
运算符试图发送到没有相应命令的 SQL 服务器有关。一旦解决方案是将ToArray()
或Tolist()
放在数据库调用的末尾,这使得该行成为获取数据的显式调用(而不是延迟调用)加载到其他每条线上)。It has something to do with the fact that the
Last
operator is trying to be sent to the SQL server which has no corresponding command. Once solution is to put aToArray()
orTolist()
at the end of your db call which makes that one line an explicit call to get the data (instead of lazing loading on each of the other lines).另一种方法是获取最后一个元素而不用 orderbydescending 并加载所有实体:
Yet another way get last element without orderbydescending and load all entities:
您可以使用
ToList()
方法将IEnumerable
转换为List
,这将确保您的所有尝试都成功,如下所示:You can convert your
IEnumerable
into aList
using theToList()
method, which will ensure that all your attempts are successful, as shown: