Apply ordering after applying Distinct
:
List<string> reps =
(from r in db.bookings
where !r.bookingRep1.Contains("*") && !r.bookingRep1.Contains(" ")
select r.bookingRep1).Distinct().OrderBy(rep => rep).ToList();
ViewBag.rep1 = new SelectList(reps, booking.bookingRep1);
When you apply Distinct
to ordered query, then ordering just removed from generated SQL:
SELECT
[Distinct1].[bookingRep1] AS [bookingRep1]
FROM ( SELECT DISTINCT
[Extent1].[bookingRep1] AS [bookingRep1]
FROM [dbo].[bookings] AS [Extent1]
WHERE [Extent1].[bookingRep1] NOT LIKE @p1 AND
[Extent1].[bookingRep1] NOT LIKE @p2
) AS [Distinct1]
When ordering is applied after Distinct
then its present in generated SQL:
SELECT
[Distinct1].[bookingRep1] AS [bookingRep1]
FROM ( SELECT DISTINCT
[Extent1].[bookingRep1] AS [bookingRep1]
FROM [dbo].[bookings] AS [Extent1]
WHERE [Extent1].[bookingRep1] NOT LIKE @p1 AND
[Extent1].[bookingRep1] NOT LIKE @p2
) AS [Distinct1]
ORDER BY [Distinct1].[bookingRep1] ASC