Search code examples
servicestackormlite-servicestack

How to get a SELECT DISTINCT on a SelectMulti query in ServiceStack OrmLite?


I'm trying to get a distinct result set of tuples, but the Distinct never gets added to query.

Example

  List<Tuple<Alpha, Beta>> results;

  var q = dbConn.From<Alpha>()
          .Join<Alpha, Beta>((a, b) => a.Id == b.AlphaId)
          ... 
          ... more joins and Wheres
          ...
          .SelectDistinct();

  results = dbConn.SelectMulti<Alpha, Beta>(q);

Adding the SelectDistinct, or not, make no difference to the outputted SQL and hence results.

How do I get SelectMulti to work with Distinct?

Thanks.


Solution

  • I've just added support for this in this commit where if .SelectDistinct() is used in the SqlExpression<T> then it will execute the SQL query using SELECT DISTINCT, e.g:

    var results = dbConn.SelectMulti<Alpha, Beta>(q.SelectDistinct());
    

    This change is available from v5.4.1 that's now available on MyGet.