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?
string.Join(" UNION ", ...)?UNION ALLinstead ofUNION: the latter deduplicates the results.string.Joinanswer below) is internally still using a loop.string.Joinis 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.