Need a function for DISTINCT with ToMergedParamsSelectStatement

I’m building a SQL with UNION, and following the practice from other posts, I’m concatenating strings like this:

var unionSql = q1.ToMergedParamsSelectStatement() 
+ " UNION " + q2.ToMergedParamsSelectStatement();

I’d like for the individual q1, q2 to have DISTINCT on them, so the resulting SQL would be something like

select distinct q1.* union distinct q2.*

Before you suggest a single distinct on the whole statement, I must say that the my actual query is more complicated than in my example, with sub selects and such, so I need the DISTINCT on each query (for reasons I can explain if necessary).

q1.SelectDistinct().ToMergedParamsSelectStatement() doesn’t do anything.

Could you provide a ToMergedParamsSelectDistinctStatement()?

That’s not going to be maintainable, just insert the distinct keyword yourself, e.g:

var distinctSql = "SELECT DISTINCT" + 
1 Like

Ok, thanks for the quick reply. Just want to avoid string manipulations for functionality provided by the ORM. Did this already, which is the same as you suggest:

q1.ToMergedParamsSelectStatement().ReplaceFirst("SELECT", "SELECT DISTINCT")