Share via


Row number in linq query

Question

Wednesday, March 12, 2014 9:31 PM

How can I add a row number column to the following query:

var query = from u in dbReport.Users
                        join p in dbReport.Physicans on u equals p.User
                        select new
                        {
                            u.Id,
                            u.FirstName,
                            u.LastName,
                            u.UserName,
                            u.Password,
                            BirthDate = u.DOB,
                            u.Sex,
                            u.Mobile,
                            u.Image,
                            p.Specialty
                        }

All replies (3)

Thursday, March 13, 2014 1:36 PM ✅Answered | 1 vote

Mohsen :

Does the below help ? Add desired out put fields accordingly

var result =  query.AsEnumerable().Select((x, index) => new { index,x.Id,x.FirstName});

Wednesday, March 12, 2014 11:44 PM

Hello,

You can do that on this way:

    int i = 1;
    var query = from u in dbReport.Users
                join p in dbReport.Physicans on u equals p.User
                let Rank = i++
                select new 
                {
                    Rank.ToString(),
                    u.Id,
                    u.FirstName,
                    ...
                };

João Sousa (MCTS) Senior Software Engineer


Thursday, March 13, 2014 9:45 AM

Hello,

You can do that on this way:

    int i = 1;
    var query = from u in dbReport.Users
                join p in dbReport.Physicans on u equals p.User
                let Rank = i++
                select new 
                {
                    Rank.ToString(),
                    u.Id,
                    u.FirstName,
                    ...
                };

João Sousa (MCTS) Senior Software Engineer

I get the following error at the 'let Rank = i++' line:

An expression tree may not contain an assignment operator.