在linq select中获取记录索引列

时间:2013-11-24 09:36:57

标签: linq select indexing

我需要在select语句中获取索引。

我的代码:

IQueryable grpdRows;
dtInput = ds.Tables[0];
grpdRows = dtInput
    .Select("", "partno")
    .AsQueryable()
    .GroupBy("new (iif(it[\"partno\"] == null, \"\", it[\"partno\"]) as GrpKey1)","it")
    .Select("new (it.Key.GrpKey1 as GrpKey1, it.Count() as TotalCount")");

如何在select语句示例中获取索引:

.Select("new (it.Key.GrpKey1 as GrpKey1, it.Count() as TotalCount, **it.Index**")");

1 个答案:

答案 0 :(得分:0)

我不确定通过Dynamic Linq可以实现这一点,但是作为解决方法,你可以尝试这样的事情:

IQueryable grpdRows;
dtInput = ds.Tables[0];
grpdRows = dtInput
    .Select("", "partno")
    .AsQueryable()
    .GroupBy("new (iif(it[\"partno\"] == null, \"\", it[\"partno\"]) as GrpKey1)","it")
    .Select("new (it.Key.GrpKey1 as GrpKey1, it.Count() as TotalCount)");

var res = (from dynamic row in grpdRows
          select row).Select((row,index)=>new {row.GrpKey1, row.TotalCount, Index=i});