2013-11-24 27 views
0

我需要在我的select語句中獲取索引。在linq 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**")"); 

回答

0

我不知道這是可能通過動態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});