0

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);
}
4
  • 1
    Flippant answer: this code doesn't compile. More serious answer? Probably not one that's as performant. Commented Nov 15, 2012 at 10:53
  • I am just trying to generate dynamic sql to create a table from code behind. The number of columns will be changing at runtime (could be 1000 some times). Being novice to LINQ i thought it may add value. Thanks all for your time Commented Nov 15, 2012 at 11:35
  • @Kiran: Are you having any issue with the current code? Why you want to do it in linq? Commented Nov 15, 2012 at 13:02
  • @huMptyduMpty: No, the current code works fine. But i thought the LINQ might be better performant. Commented Nov 15, 2012 at 13:15

4 Answers 4

3
Enumerable.Range(2, noOfColumns+1)
  .ToList()
  .ForEach(i => 
     sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity" columnLength));

But I don't think it looks better :-).

Sign up to request clarification or add additional context in comments.

3 Comments

I've removed my -1 because I'm pretty sure you're joking :)
It is just an answer for the question :-) I hope it will tell author that it's not necessary to use LINQ everywhere
@Nagg: I am not sure how this part doesn't comes up with any errors "commodity" columnLength
1

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.

5 Comments

TIL that String.Join uses a StringBuilder behind the scenes.
@Rawling: Yes, you're right. Removed the "without StringBuilder".
That wasn't a criticism, I'd just never thought about it and thought Join (at least the array version) would allocate space for the whole thing and then fill it.
@Rawling: I've noticed it recently. But the string.Join(string[]) overload uses a very efficient copy method memcpyimpl instead of a StringBuilder.
@TimSchmelter:I am just trying to generate dynamic sql to create a table from code behind. The number of columns will be changing at runtime (could be 1000 some times). Being novice to LINQ i thought it may add value. Thanks all for your time.
1

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.

Comments

0

Try This,

Enumerable.Range(2, noOfColumns).ToList().ForEach(r => { sql.AppendFormat(" [{0}{1}] [varchar]({2}),", "commodity" columnLength); });

4 Comments

You're right, taking off the +1 fixes a range error, doesn't it? Please accept my apologies. My downvote's locked in, but if you edit your answer (you could put some linebreaks in!) then I can remove it.
@Swani: Seems this doesn't compile !!
@huMptyduMpty, The question itself not compile ('[{0}{1}] [varchar]({2}),", "commodity" columnLength); '), if you ignore that part.. rest is complied code :).
@Swani: So it mean it is fare to put something which not work as an answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.