不支持 Last 和 LastOrDefault

发布于 2024-12-02 09:03:09 字数 945 浏览 1 评论 0原文

我正在尝试获取列表中的第一个和最后一个值。支持查询运算符 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

人海汹涌 2024-12-09 09:03:09
  • 我更愿意使用 AsEnumerable(),而不是通过调用 ToList()ToArray() 将其放入自己的列表中。
  • 另外,像其他人一样,您应该尝试OrderByDescending()
  • 而不是Count(),我会使用Any()
  • Instead of putting it into an own list by calling ToList() or ToArray() i would prefer to use AsEnumerable().
  • Additionally like the others you should try OrderByDescending()
  • Instead of Count() i would use Any().
寄居人 2024-12-09 09:03:09

要么将 OrderBy 切换为

.OrderByDescending(p => p.BillID)

(并首先使用),要么执行类似的操作(

purchaseBills.ToArray().Last()

如果这不是很昂贵)。

either you switch your OrderBy to

.OrderByDescending(p => p.BillID)

(and use first) or you do something like

purchaseBills.ToArray().Last()

if this is not to expensive.

回眸一遍 2024-12-09 09:03:09

后端数据库不支持Last。您应该尝试其他技术:

  1. 使用 OrderByDescending 运行查询,以便您请求的项目排在前面。

  2. 照常编码您的 LINQ 查询,但强制 Linq2Sql 将其呈现到 CLR 集合,然后您就可以自由访问本地的所有内容,包括 Last。示例:

    var bills = buyBills.ToList();
    var 最后 = bills.Last();
    

Last is not supported by the back-end DB. You should try other techniques:

  1. Run your query using OrderByDescending so your requested item comes first.

  2. 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:

    var bills = purchaseBills.ToList();
    var last = bills.Last();
    
玻璃人 2024-12-09 09:03:09

问题是 LastReverse 无法轻松转换为 SQL,因此要么将其转换为内存中的内容(ToListToArray) 如果记录不会太多,或者您可以第二次运行查询,使用 OrderByDescending 而不是 OrderBy 并使用 <代码>首先。

The problem is that there's no easy translation into SQL for Last or Reverse, 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, with OrderByDescending instead of OrderBy and use First.

月亮是我掰弯的 2024-12-09 09:03:09

我尝试不使用 LastOrDefault() 因为有时它不起作用或不支持。
我按 descid 进行排序,然后获取第一条记录。

.OrderByDescending(o=>o.id)
.FirstOrDefault(s => s.Name == Name)

I try not to use LastOrDefault() because sometime it doesn't work or support.
I'm sorting id by desc and then grab the first records.

.OrderByDescending(o=>o.id)
.FirstOrDefault(s => s.Name == Name)
笑饮青盏花 2024-12-09 09:03:09

这与 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 a ToArray() or Tolist() 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).

过去的过去 2024-12-09 09:03:09

另一种方法是获取最后一个元素而不用 orderbydescending 并加载所有实体:

var lastBill = purchaseBills
    .Where(f => f.BillID == purchaseBills.Max(f2 => f2.BillID))
    .FirstOrDefault();

Yet another way get last element without orderbydescending and load all entities:

var lastBill = purchaseBills
    .Where(f => f.BillID == purchaseBills.Max(f2 => f2.BillID))
    .FirstOrDefault();
偏爱你一生 2024-12-09 09:03:09

您可以使用 ToList() 方法将 IEnumerable 转换为 List,这将确保您的所有尝试都成功,如下所示:

var purchaseBills = db.PurchaseBills.OrderBy(p => p.BillID);

if (purchaseBills.Any())
{
  var firstBill = purchaseBills.First(); // This is supported

// Attempt 1                    
  var lastBill = purchaseBills.ToList().Last();

// Attempt 2
  var lastBill = purchaseBills.ToList().LastOrDefault(); 

//NoLogic
  var lastBill = purchaseBills.ToList().Reverse().First();

  textBoxPurchaseBillFrom.Text = firstBill.BillNo.ToString();
  textBoxPurchaseBillTo.Text = lastBill.BillNo.ToString();
}

You can convert your IEnumerable into a List using the ToList() method, which will ensure that all your attempts are successful, as shown:

var purchaseBills = db.PurchaseBills.OrderBy(p => p.BillID);

if (purchaseBills.Any())
{
  var firstBill = purchaseBills.First(); // This is supported

// Attempt 1                    
  var lastBill = purchaseBills.ToList().Last();

// Attempt 2
  var lastBill = purchaseBills.ToList().LastOrDefault(); 

//NoLogic
  var lastBill = purchaseBills.ToList().Reverse().First();

  textBoxPurchaseBillFrom.Text = firstBill.BillNo.ToString();
  textBoxPurchaseBillTo.Text = lastBill.BillNo.ToString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文