如何使表达式添加1,2,3,4等的计数器(ID)?我只想拥有一些识别数据的非常密钥
var query = data.Select(x =>
new DemoItemV1
{
Id = x.Field<double>("ID"),
AreaId = x.Field<int>("1Area ID"),
CategoryTitle = x.Field<string>("2Table Title")
}).ToList();
答案 0 :(得分:1)
我认为您不能完全在Linq-to-Sql或Linq-to-Entities查询中执行此操作。但是,由于您已将其实现为列表,因此可以执行此操作:
var query = data.Select(x =>
new DemoItemV1
{
AreaId = x.Field<int>("1Area ID"),
CategoryTitle = x.Field<string>("2Table Title")
})
.AsEnumerable()
.Select((x, i) => { x.ID = i + 1; return x })
.ToList();