我想在使用Linq to Sql读取的数据集中添加一个唯一的Id到每个记录



这是我的阅读:

    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();

他们的数据集没有唯一的标识符来标识单个记录,所以我想添加一个。我希望第一列是这个标识符-它可以基于行数。我怎样才能做到这一点呢?

您可以将查询投影移动到客户端并使用Enumerable的重载。选择:

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();

相关内容

  • 没有找到相关文章

最新更新