Linq:选择Distinct and Order

时间:2014-02-14 13:27:22

标签: vb.net linq-to-sql

我有以下......

Dim dc as New DBDataContext
Dim q = From c In dc.Customers _
    Order By c.City _
    Select c.City _
    Distinct

我的问题是虽然查询产生了一个不同的列表但它没有排序。我如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

Order By c.City修复后会移动Distinct吗?

Dim q = From c In dc.Customers _
    Select c.City _
    Distinct _
    Order By City

或如果以上不起作用,您可以在客户端进行订购操作:

Dim result = q.OrderBy(Function(x) x)

答案 1 :(得分:1)

如果q是未排序的查询,那么您可以在之后对其进行排序

Dim sorted = From c in q order by c select c 

q = From c in q order by c select c