Linq在ORDER BY中自定义排序,其余由asc排序

时间:2017-03-10 09:53:56

标签: vb.net linq

我有一个带有companyNames的字符串列表,

1. CompanyA
2. CompanyB
3. CompanyA
4. CompanyC

我的方法是:

 Dim companyNames = From cp In companyList 
                    Order By If(cp.CompanyName = 'CompanyC', 1, 0) Descending
                    Select cp.CompanyName
                    Distinct

但我得到了结果:

1. CompanyC
1. CompanyB
2. CompanyA

我希望在设置“CompanyC”之前按公司名称订单保留订单。 ,我想要以下结果

 1. CompanyC
 2. CompanyA
 3. CompanyB

1 个答案:

答案 0 :(得分:5)

您需要CompanyName Ascending的其他订单:

Dim companyNames = 
    From cp In companyList 
    Order By If(cp.CompanyName = "CompanyC", 1, 0) Descending, CompanyName Ascending
    Select cp.CompanyName
    Distinct