dinotom
dinotom

Reputation: 5162

order results of linq query by multiple items

using the following query

 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

I get the results I want, but i'd like to further order them by description and time

I tried the following, which should have worked

 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})

I also tried ThenBy which the compiler didnt like. Any help on how to accomplish this would be appreciated. Essentially I want a sort order of time then description then symbol.

Upvotes: 1

Views: 160

Answers (2)

Maxime R.C
Maxime R.C

Reputation: 179

You can try ordering your items at the end of the query like that :

 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)

It's probably faster to do it in the query tho.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564333

When using integrated syntax, you'd write this as:

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

For details, see the Query Expression Examples for ThenBy.

Upvotes: 2

Related Questions