VB.NET - LINQ to SQL - How do I add in Row_Number

2019-05-08 15:00发布

问题:

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

How can I convert this solution to 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

I'm having trouble porting that last line. I have been unable to locate a VB.NET example.

I'm actually not looking for any paging functionality like the example provides, just good old-fashioned Row_Number(Order By X) row index.

回答1:

LinqToSql can't do a Row_Number(Order By X) in the database.

The c# code you posted does it against in-memory instances by calling Enumerable.Select

On the msdn page for Enumerable.Select, there's a vb.net example.


Here is a working example that demonstrates the projection of a row index in 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