0

I need to build a my sql query which is basically at his core is similar to the following:

select something from {myCurrentTable} where some condition

This query must be repeated N times where N is unknown and put in a union.

I need to change the value of myCurrentTable every iteration so i would do something like this:

foreach(string table in myTables)
{
    queryTot += $"SELECT something from {table} where some condition";
    if(!lastIteration)
       queryTot += " union ";
}

Is there any function i can use to get rid of the explicit loop?

10
  • Is there a reason you need to get rid of the loop? Have you thought about string.Join(" UNION ", ...) ? Commented Sep 20, 2021 at 14:04
  • For what it's worth you may want UNION ALL instead of UNION: the latter deduplicates the results. Commented Sep 20, 2021 at 14:09
  • @BurnsBA mainly cause i want to make the part of the function i'm working on less "verbose" Commented Sep 20, 2021 at 14:11
  • 1
    Also, what is wrong with using a loop in the first place? Any code you do use (like the string.Join answer below) is internally still using a loop. Commented Sep 20, 2021 at 14:16
  • 1
    The thing that's nice about using string.Join is that you don't need to be concerned with checking for the last iteration, and it also works with a single table :). With the loop approach, both these need to be accounted for. Commented Sep 20, 2021 at 14:21

2 Answers 2

3

You could use LINQ Select to project the tables into SQL select statements, then string.Join to add a union between each one:

string query = string.Join(
    " union ",
    myTables.Select(table => $"SELECT something from {table} where some condition"));

Using string.Join rather than a manual loop means that the unions are correctly inserted without having to check for the last iteration, or check whether the myTables collection only contains a single item.

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

2 Comments

Thank you very much. I have used string.Join in the past but i didn't thought i could have combined with LINQ Select.
@DanieleSartori Select returns an IEnumerable<string> here, and string.Join has an overload that accepts IEnumerable<string> as the second argument.
2

If you concatenate strings in a loop in an undeterministic count, you should consider using StringBuilder instead of regular String.

Since a string is an immutable data type in C#, when you combine two or more strings, a new string is produced. If you append a series of characters to a string (like in your example), you will recreate the same string in memory multiple times..

An alternative approach is modifying a StringBuilder instance which does not result in the creation of a new instance in memory, hence it's recommended in the case you describe.

Comments

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.