Suppose:
var a = SomeCollection.OrderBy(...)
.Select(f => new MyType
{
counter = ? //I want counter value, so what first
//object have counter=1, second counter=2
//and so on
a=f.sometthing,
...
});
How do I set this counter value? Or do I fave to iterate a
afterwards?
Use the overload of Select that gives you the current element's 0-based index.
.Select((item, index) => new MyType
{
counter = index + 1,
a = item.Something
Simply capture a variable:
int index = 1;
var a = SomeCollection.OrderBy(...)
.Select(x => new MyType { counter = index++; });
The counter will increment as each iteration is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With