Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' Visual Basic Programming 24-Hour Trainer by Rod Stephens; ISBN: 978-0-470-94335-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' Visual Basic Programming 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 22nd, 2014, 11:50 AM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
Default How to improve the LINQ query performance within a for loop

Hi,

I am trying to improve the speed of my application by getting rid of the LINQ queries inside my for loops (very inefficient). Do you know the best way to do it? Should I define a local IQueryable equal to the LINQ query result and then read data from that object?

Here is a piece of my code where I have loop of a LINQ query (For Each wkend ) within another for loop (For j). It is very slow.

Code:
wkendQuery = From wkendSchedule As MilestoneWeekendSchedule In eReportContext.MilestoneWeekendSchedules Where wkendSchedule.milestoneStatusID.Equals(thisMilestoneID)

For j = 0 To dgvWaterfall.Columns.Count - 1
                If wkendQuery.ToArray.Length >= 0 Then
                    For Each wkend In wkendQuery.ToArray
                        If wkend.weekendDate = dgvWaterfall.Columns(j).HeaderText Then
                                 ...
                        End if
                    next wkend
                End if
next j
Thanks very much for your help and support.

Last edited by shabnamzangeneh; July 22nd, 2014 at 11:58 AM.
Reply With Quote
  #2 (permalink)  
Old July 22nd, 2014, 05:02 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,145, Level: 23
Points: 3,145, Level: 23 Points: 3,145, Level: 23 Points: 3,145, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 641
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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

Quote:
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:

Code:
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

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old July 22nd, 2014, 07:01 PM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Quote:
My guess is you'll see a big improvement just by moving the call to ToArray.
That was a BIG improvement (~80%).

Thanks for your help.
Reply With Quote
  #4 (permalink)  
Old July 30th, 2014, 12:49 AM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

I am better with c# so ill show in c#. Not knowing exactly what your gridview looks like, I am assuming its dates as column headers based on your for loops. I am also free handing this, so it may contain some compile errors. The point is how i am doing it.


Code:

// Select all column header text as dates
var gvColumnDates = dgvWaterfall.Columns.Select( c => Convert.ToDateTime(c.HeaderText) );

// now select the schedules that have dates in the gridview headers that have someId as milestonestatus id
var queryResults = eReportContext.MilestoneWeekendSchedules.Where( s => s.MilestoneStatusId = someid && gvColumnDates.Contains( s.WeekendDATE ) ).ToArray();

Last edited by mmorgan30; July 30th, 2014 at 12:53 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Better way to improve performance while loading bulk data in reports Kamakshi K XSLT 0 February 12th, 2014 08:28 AM
How to improve the performance of a Gridview that contains 10000 data records in c# a kailasam ASP.NET 4.5 General Discussion 3 May 31st, 2013 04:49 AM
Comparing Performance Linq To entities and outofBand SQL updates urpcor BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0 4 September 28th, 2009 06:22 AM
How to improve performance with VBA and Powerpoint ravan76 Excel VBA 0 June 10th, 2007 11:29 PM
How do i improve the performance of report syedjavid Crystal Reports 4 October 26th, 2003 04:46 AM



All times are GMT -4. The time now is 03:25 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.