2014-02-14 54 views
0

我有以下....LINQ的:SELECT DISTINCT和秩序

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

我的問題是,雖然查詢產生不同的列表是沒有排序。我如何做到這一點。

回答

1

如果q您未排序的查詢,那麼你可以事後進行排序它

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

q = From c in q order by c select c 
1

威爾移動後Distinct修復它Order By c.City

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

,或者如果以上不工作,你可以在客戶端做排序操作:

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

對不起,第一個選項是不允許在Linq和t他的第二個選擇恐怕你已經失去了我的功能(x)x位... – Mych

+0

更新了我的第一個選項語法。關於第二,這是lambda表達式。 'Function(x)'表示將'q'中的項目聲明爲'x'(本例中爲City),然後選擇'x'作爲'OrderBy'函數的參數。簡而言之,「按x'排序」,意思是「按城市排序」。 – har07

+0

關於[在VB.NET中的Lambda表達式](http://msdn.microsoft.com/zh-cn/library/bb531253.aspx) – har07