How to add a serial number field Using Linq

jewel 1,206 Reputation points
2024-08-08T09:36:26.41+00:00
I want a new field in my query to display a auto serial number.

  var data = from a in _context.tbl_Employees
             select new
             {
                 id = a.EmployeeID,
                 name = a.EmployeeName,
                 address = a.Address,
                 Slno =  // How Can Create?
           };
Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2024-08-08T09:53:44.14+00:00

    Try this:

    var data = _context.tbl_Employees.Select( ( a, i ) => 
                   new
                   {
                     id = a.EmployeeID,
                     name = a.EmployeeName,
                     address = a.Address,
                     Slno = i + 1
                   } );
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.