在LINQ查询中忽略'排序依据'

时间:2013-03-26 14:20:22

标签: vb.net linq sql-order-by

我正在尝试按公司名称对列表进行排序。我尝试了以下代码,但这是按CompID排序列表而不是CoShort。我应该如何更改它以按CoShort排序?

Public Shared Function [SelectCompanyData](iElement() As Integer) As List(Of CompanyList)

    Dim db As New EntryDataContext()

    Dim q As IQueryable(Of CompanyList) = (From Act_Sub_Manfu_Link In db.Act_Sub_Manfu_Links _
            Join Company In db.Companies _
            On Act_Sub_Manfu_Link.CompID Equals Company.CompID _
            Where iElement.Contains(Act_Sub_Manfu_Link.ACCN) _
            And Company.In_Dir _
            Select New CompanyList With { _
                .CompID = Company.CompID, _
                .InDir = Company.In_Dir, _
                .CoShort = Company.CoShort _
                }).Distinct

    q.OrderBy(Function(c) c.CoShort)

    Dim list As List(Of CompanyList) = q.ToList

    Return list

End Function

2 个答案:

答案 0 :(得分:6)

您必须将有序集合分配到变量中:

Dim oq As IOrderedQueryable(Of CompanyList) = q.OrderBy(Function(c) c.CoShort)

并使用它来获得结果列表:

Dim list As List(Of CompanyList) = oq.ToList()

答案 1 :(得分:1)

除了返回值

之外,不需要分配任何内容
Public Function SelectCompanyData(iElement() As Integer) As List(Of CompanyList)
    Dim db As New EntryDataContext()
    Return (From Act_Sub_Manfu_Link In db.Act_Sub_Manfu_Links _
            Join Company In db.Companies _
            On Act_Sub_Manfu_Link.CompID Equals Company.CompID _
            Where iElement.Contains(Act_Sub_Manfu_Link.ACCN) _
            And Company.In_Dir _
            Select New CompanyList With { _
                .CompID = Company.CompID, _
                .InDir = Company.In_Dir, _
                .CoShort = Company.CoShort _
                }).Distinct().OrderBy(Function(c) c.CoShort).ToList()
End Function