none
How To Speed Up Linq Query RRS feed

  • Question

  • Hello I am brand new to LINQ and was wondering how I could improve the speed of the following query or possibly the function itself?

    Any help will be appreciated.

        Public Function GetIncident(ByVal BusName As String) As List(Of IncidentRecord) Implements IService1.GetIncident
    
            Dim incidents = (From incident In db.incidents Where incident.bus_name.Contains(BusName) _
                             Order By incident.bus_name _
                             Select incident.inc_address, incident.bus_name, incident.inc_no)
    
            Dim list As New List(Of IncidentRecord)
            For Each c In incidents
                list.Add(New IncidentRecord With
                         {.Address = c.inc_address, _
                          .BusinessName = c.bus_name, _
                          .IncidentNumber = c.inc_no})
            Next
    
            Return list
        End Function


    Brett The Jet

    Friday, May 18, 2012 4:26 PM

Answers

  • You aren't going to be able to do a lot about the speed, as the query is fairly efficient - but you can simplify this and build the result directly:

    Public Function GetIncident(ByVal BusName As String) As List(Of IncidentRecord) Implements IService1.GetIncident
    
            Return (From incident In db.incidents _
                    Where incident.bus_name.Contains(BusName) _
                    Order By incident.bus_name _
                    Select New IncidentRecord With
                         { .Address = incident.inc_address, _
                           .BusinessName = incident.bus_name, _
                           .IncidentNumber = incident.inc_no}) _
                 .ToList()
    End Function

    This will prevent the creation of the extra objects and save you an extra loop...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by brmcdani44 Friday, May 18, 2012 6:40 PM
    Friday, May 18, 2012 6:20 PM

All replies

  • You aren't going to be able to do a lot about the speed, as the query is fairly efficient - but you can simplify this and build the result directly:

    Public Function GetIncident(ByVal BusName As String) As List(Of IncidentRecord) Implements IService1.GetIncident
    
            Return (From incident In db.incidents _
                    Where incident.bus_name.Contains(BusName) _
                    Order By incident.bus_name _
                    Select New IncidentRecord With
                         { .Address = incident.inc_address, _
                           .BusinessName = incident.bus_name, _
                           .IncidentNumber = incident.inc_no}) _
                 .ToList()
    End Function

    This will prevent the creation of the extra objects and save you an extra loop...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by brmcdani44 Friday, May 18, 2012 6:40 PM
    Friday, May 18, 2012 6:20 PM
  • Thanks Reed, I was wondering if that loop was the culprit.  

    Brett The Jet

    Friday, May 18, 2012 6:41 PM