Reputation: 441
Is there is a LINQ expression equivalent to below code?
int noOfColumns = 10;
for (int i = 2; i <= noOfColumns+1; i++)
{
sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity", columnLength);
}
Upvotes: 0
Views: 1502
Reputation: 5328
The parameters of Enumerable.Range
are start
and count
. This means that it does not directly replace for loops.
for(var i = 5; i < 8; ++i)
will produce the numbers from 5 to 7.
Enumerable.Range(5, 8)
will produce the numbers from 5 to 12 (5+8-1).
To get the desired effect, Enumerable.Range(start, end - start)
would need to be used.
Upvotes: 1
Reputation: 6563
Try This,
Enumerable.Range(2, noOfColumns).ToList().ForEach(r => { sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity" columnLength); });
Upvotes: 0
Reputation: 460340
For what it's worth, the (compiling) Linq replacement would be:
IEnumerable<string> cols = Enumerable.Range(2, noOfColumns +1)
.Select(i => string.Format(" [{0}] [varchar]({1})", "commodity", columnLength));
string sql = string.Join(",", cols);
But i must admit that i have no idea what you're doing.
Upvotes: 1
Reputation: 6142
Enumerable.Range(2, noOfColumns+1)
.ToList()
.ForEach(i =>
sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity" columnLength));
But I don't think it looks better :-).
Upvotes: 3