按多个项目对linq查询的结果进行排序

时间:2013-06-26 17:20:10

标签: linq entity-framework-5

使用以下查询

 Dim allEvents As New List(Of Events)
 Dim digits = "1 2 3 4 5 6 7 8 9 0".Split()
 Dim results = (From t In ctx.events
                           Where t.date = todaysdate.Date AndAlso
                           Not digits.Any(Function(d) (t.symbol.Contains(d)))
                           Order By t.time
                           Select New With {
                                  t.symbol,
                                  t.date, t.time,
                                  t.description,
                                  t.domestic_call_num,
                                  t.web_url})

             If results.Count > 0 Then
                For x As Integer = 0 To results.Count - 1
                    Dim newevent As New Events
                    With newevent
                        .CompanySymbol = results(x).symbol
                        .EventDate = results(x).date
                        .EventTime = Left(results(x).time.ToString(), 5)
                        .EventDescription = results(x).description
                        If results(x).domestic_call_num IsNot Nothing Then
                            .DialInNumber = results(x).domestic_call_num
                        Else
                            .DialInNumber = ""
                        End If
                        If results(x).web_url IsNot Nothing Then
                            .WebCastUrl = results(x).web_url
                        Else
                            .WebCastUrl = ""
                        End If
                    End With
                    allEvents.Add(newevent)
                Next
                Return allEvents

我得到了我想要的结果,但我想按描述和时间进一步订购

我尝试了以下哪些应该有效

 Dim results = (From t In ctx.events
                           Where t.date = todaysdate.Date AndAlso
                           Not digits.Any(Function(d) (t.symbol.Contains(d)))
                           Order By t.time
                           Group By t.description
                           Select New With {
                                  t.symbol,
                                  t.date, t.time,
                                  t.description,
                                  t.domestic_call_num,
                                  t.web_url})

我也试过了编译器不喜欢的ThenBy。任何有关如何实现这一点的帮助将不胜感激。基本上我想要一个时间顺序然后描述然后符号。

2 个答案:

答案 0 :(得分:2)

使用集成语法时,您可以将其写为:

Order By t.time, t.description
Select New With {
     ...

有关详细信息,请参阅Query Expression Examples for ThenBy

答案 1 :(得分:1)

您可以尝试在查询结尾处订购商品,如下所示:

 Dim results = (From t In ctx.events
                       Where t.date = todaysdate.Date AndAlso
                       Not digits.Any(Function(d) (t.symbol.Contains(d)))
                       Select New With {
                              t.symbol,
                              t.date, t.time,
                              t.description,
                              t.domestic_call_num,
                              t.web_url}).OrderBy(Function(r) r.time)
                                         .ThenBy(Function(r) r.description)

在查询中这样做可能会更快。