我正在尝试获取列表中的第一个和最后一个值.支持查询运算符 First(),但 Last() 和 LastOrDefault() 会出错.我是否错误地使用了 Last() 运算符?
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();
}
更新:
--错误--
尝试 1:不支持查询运算符Last".
Attempt 1: The query operator 'Last' is not supported.
尝试 2:不支持查询运算符LastOrDefault".
Attempt 2: The query operator 'LastOrDefault' is not supported.
尝试 3:不支持查询运算符Reverse".
Attempt 3: The query operator 'Reverse' is not supported.
AsEnumerable(),而不是通过调用 ToList() 或 ToArray() 将其放入自己的列表中.OrderByDescending() Any() 而不是 Count().ToList() or ToArray() i would prefer to use AsEnumerable().OrderByDescending() Count() i would use Any().这篇关于不支持 Last 和 LastOrDefault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么我不应该总是在 C# 中使用可空类型Why shouldn#39;t I always use nullable types in C#(为什么我不应该总是在 C# 中使用可空类型)
C# HasValue vs !=nullC# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET:空值和 DbNull —— 有没有更高效的语法C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有没有更高效的语法?)
如何在c#中将空值设置为int?How to set null value to int in c#?(如何在c#中将空值设置为int?)
使用 Min 或 Max 时如何处理 LINQ 中的空值?How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 时如何处理 LINQ 中的空值?)
在 C# 中如果不为 null 的方法调用Method call if not null in C#(在 C# 中如果不为 null 的方法调用)