【问题标题】:VB.NET - LINQ to SQL - How do I add in Row_Number to a LINQ to SQL query in VB.NET?VB.NET - LINQ to SQL - 如何将 Row_Number 添加到 VB.NET 中的 LINQ to SQL 查询?
【发布时间】:2011-08-31 14:03:16
【问题描述】:

How do I add ROW_NUMBER to a LINQ query or Entity?

如何将此解决方案转换为 VB.NET?

var start = page * rowsPerPage;
Products.OrderByDescending(u => u.Sales.Count())
    .Skip(start)
    .Take(rowsPerPage)
    .AsEnumerable()
    .Select((u, index) => new { Product = u, Index = index + start }); // this line gets me

我在移植最后一行时遇到问题。我一直找不到 VB.NET 示例。

我实际上并不是在寻找示例提供的任何分页功能,只是很好的老式 Row_Number(Order By X) 行索引。

【问题讨论】:

  • 本例中 OrderByDescending(u => u.Sales.Count()) 的目的是什么?由于在查询期间计数不会改变,因此除了自旋周期(假设 SQL 没有在执行计划之外对其进行优化)之外,这实际上什么都不做。
  • 我不知道。这是来自另一个 Stackoverflow 帖子的代码 sn-p。我有兴趣将行索引投影到 VB.NET 中的匿名对象列表中
  • 在这种情况下,使用下面提到的 Select David B 中的重载。

标签: vb.net linq-to-sql


【解决方案1】:

LinqToSql 不能在数据库中做 Row_Number(Order By X)。

您发布的 c# 代码通过调用 Enumerable.Select 对内存中的实例执行此操作

在 Enumerable.Select 的 msdn page 上,有一个 vb.net 示例。


这是一个演示 VB.NET 中行索引投影的工作示例:

Dim R = (
     From P In DB.Products Select P
  ).AsEnumerable().Select(
     Function(Product, index) New With {index, Product}
  )

Response.Write(R(12).Product.Doc_width.ToString()) 'Access object members
Response.Write(R(12).index.ToString() 'Access Row Index

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多