4

This is my read:

    OnSiteV3DataContext _v3Context = new OnSiteV3DataContext();
        var dataset1 = (from sp in _v3Context.SQLPendingEvents
                        where 
                        (sp.EventType.ToString() == str100w)
                        select new
                        {

                            sp.KeyDeviceID,
                            sp.UpdateTime ,
                            sp.EventClass ,
                            sp.EventCode ,
                            sp.EventType ,
                            sp.EventDateTime ,
                            sp.KeyPropertyID,
                            sp.KeyEntityID,
                            sp.EventText,
                            sp.KeyUserID,
                            sp.EventImage,
                            sp.TaskType ,
                            sp.TaskSubType ,
                            sp.UniqueTaskID 
                        }).ToList();

    dataset1 = dataset1.Where(sp => !sp.KeyDeviceID.ToString().Contains('~')).ToList();
        gvSqlPendingEvent.DataSource = dataset1;
        gvSqlPendingEvent.DataBind();

Ther dataset has no unique identifier to identify individual records, so I would like to add one. I want the first column to be this identifier - it could be based on the rowcount. How can I achieve this?

Steve Staple
  • 2,983
  • 9
  • 38
  • 73
  • check this answer http://stackoverflow.com/questions/12025170/how-can-i-add-a-unique-row-number-to-my-linq-select – Overmachine Mar 14 '14 at 16:18
  • Possible duplicate of [How To Project a Line Number Into Linq Query Results](http://stackoverflow.com/questions/365086/how-to-project-a-line-number-into-linq-query-results) – Michael Freidgeim Jul 13 '16 at 03:24

1 Answers1

3

You can move query projection to client side and use overload of Enumerable.Select:

var dataset1 = _v3Context.SQLPendingEvents
                         .Where(sp => sp.EventType.ToString() == str100w)
                         .AsEnumerable() // further processing occurs on client
                         .Select((sp,index) => new {
                             ID = index + 1,
                             sp.KeyDeviceID,
                             sp.UpdateTime ,
                             sp.EventClass ,
                             sp.EventCode ,
                             sp.EventType ,
                             sp.EventDateTime ,
                             sp.KeyPropertyID,
                             sp.KeyEntityID,
                             sp.EventText,
                             sp.KeyUserID,
                             sp.EventImage,
                             sp.TaskType ,
                             sp.TaskSubType ,
                             sp.UniqueTaskID 
                         }).ToList();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459