I'm trying to ORDER BY
some fields but SELECT DISTINCT
by another field, and after that apply a Skip & Take
Code:
var result = entities.OrderBy(e => e.OrderField)
.Select(e => e.SelectField)
.Distinct()
.Skip(skip)
.Take(take)
.ToArray();
But I'm getting this exception:
System.NotSupportedException: 'The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.'
I understand the reason (the set is being changed between the ordering and the skipping) but can I work around this issue without querying the database more than once (edit: and not fetching more entities than its supposed to)?
EDIT: I don't think there's an optimal solution for that, so what I did was fetch the values and then paginated them. *sigh*
var result = entities.OrderBy(e => e.OrderField)
.Select(e => e.SelectField)
.Distinct()
.ToArray()
.Skip(skip)
.Take(take)
.ToArray();