View Single Post
Old July 22nd, 2014, 05:02 PM
Rod Stephens's Avatar
Rod Stephens Rod Stephens is offline
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

Yes, LINQ can be inefficient, particularly if it's inside a loop as it is here.

Should I define a local IQueryable equal to the LINQ query result and then read data from that object?
That's probably a good idea.

The wkendQuery query doesn't change inside the loop. But when you execute its ToArray method (inside the loop), it executes the query and moves the result into an array each time. You might think it would be smart enough to keep the array around, but it's not and rebuilds it each time.

So you can probably get a big improvement if you just move the ToArray call outside of the loop. Something like this:

wkendQuery = From wkendSchedule As MilestoneWeekendSchedule
     In eReportContext.MilestoneWeekendSchedules
     Where wkendSchedule.milestoneStatusID.Equals(thisMilestoneID)
MilestoneWeekendSchedule[] weekends = wkendQuery.ToArray()

For j = 0 To dgvWaterfall.Columns.Count - 1
    If wkendQuery.ToArray.Length >= 0 Then
        For Each wkend In weekends
            If wkend.weekendDate = dgvWaterfall.Columns(j).HeaderText Then
            End If
        Next wkend
    End If
Next j
If that isn't a big enough improvement, then you'll need to look at other ways to store the milestone weekend schedules from which you're pulling data. For example, if you store the data in a sorted list or database with an index on the milestoneStatusID field, you may be able to find the data you need more quickly than the sequential search that LINQ is probably performing.

(My guess is you'll see a big improvement just by moving the call to ToArray.)

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)