1

How can I make the expression add a counter (ID) that is 1,2,3,4 etc... ? I just want to have some unqiue key to identify the data

     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();
punkouter
  • 5,170
  • 15
  • 71
  • 116
  • take look this one. it shows how to add index in. http://stackoverflow.com/questions/365086/how-to-project-a-line-number-into-linq-query-results – John Liu Mar 28 '13 at 17:22

1 Answers1

1

I don't think you can do this purely in a Linq-to-Sql or Linq-to-Entities query. But, since you're already materializing it to a list, you can do this:

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();
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331