I’ve got two tables that I need to UNION, and found a recommendation to use ToSqlStatement on both and concatenate with a " UNION ALL " string.
Howerver, the column name differs, so on one table I’ll need SELECT myCol as otherName.
Yes, that’s the answer I was referring to, but my question is how to rename columns? Because when you do a UNION the columns must have the same name… (it may seem strange to union between two tables, but I know what I’m doing).
Now I got another problem, which is related to the UNION.
Using the method you suggested, with q1.ToSelectStatement() + " UNION ALL " q2.ToSelectStatement(),
I get trouble if both statements have variables. The “@0” appears twice…
I’m merging a column from two tables, and the column is not called the same in the two tables, even if it’s the same info:
var query1 = db.From<Table1>().Where("....").Select(x => x.TheColumn)
var query2 = db.From<Table2>().Where("....").Select(y => new { TheColumn = y.OtherName });
var combinedSql = query1.ToMergedParamsSelectStatement() + " UNION " + query2.ToMergedParamsSelectStatement(); // UNION ALL gives duplicates, which I don't want
var mergedResults = db.Column<int>(combinedSql);