Can I have an incrementing count variable in LINQ?

后端 未结 3 1047
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 23:57

I want to do something like this:

from a in stuff
let counter = 0
select new { count = counter++, a.Name };

But I get a error telling me that c

3条回答
  •  别那么骄傲
    2021-02-05 00:11

    If you truly want it to be a counter, and not just an index, then just move the counter declaration outside the LINQ expression

    var counter = 0;
    from a in stuff
    select new { count = counter++; a.Name };
    

提交回复
热议问题