Linq用select创建索引

本文关键字:索引 创建 select Linq | 更新日期: 2023-09-27 18:12:17

我有这样的查询:

var query = (from x in be.tblEntries
             select new { x.Entry_PK, x.EntryStatus }).ToList();
var results = query.Select((index, x) => new { index, x });

创建索引列,问题是它创建的是二维数组

{Index=1,{ Entry_PK = 32432, x.EntryStatus  =true}}

有没有一种方法可以让它像这样将索引附加到一维中:

{Index=1, Entry_PK = 32432, x.EntryStatus  =true}

Linq用select创建索引

不选择对象,而是选择其属性,如:

var results = query.Select((index, x) => new { index, x.Entry_PK, x.EntryStatus });

如果你的集合是一个内存集合,你可以这样做:

var query = be.tblEntries
              .Select((x, i) => new { Index = i, x.Entry_PK, x.EntryStatus })
              .ToList();