Click here to Skip to main content
13,007,563 members (60,602 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Can anyone please suggest me why my linq query taking time

lstSearch = (From s In lstGetResult
     Where (s.CODE.StartsWith(txtCode.Text.Trim(), StringComparison.OrdinalIgnoreCase)) OrElse
     (s.NAME.StartsWith(txtCode.Text.Trim(), StringComparison.OrdinalIgnoreCase))
     Select New SPGet_Result With {.CODE = s.CODE, .NAME = s.NAME}).ToList()

Thanks in advance :)
Posted 3-Jan-13 20:31pm
Suvabrata Roy 4-Jan-13 2:35am
lstGetResult how many entity's are there
URVISHSUTHAR 4-Jan-13 2:59am
single entity with 5 to 6 thousand rows and I wanted to filter it on Textbox changed event
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

try to split the query to find the slowest part.

Note : the startswith, i think to remember, is quite slow, may be its faster a contains or even a indexof, the result is that you search the whole part, not the begining, so the rendering could be slower, if you don't want that, you can make a "equals" comparison between your txtCode and a substring(0, length) of the CODE/NAME, and test if is faster than statswith.

var stopWatch = new System.Diagnostics.Stopwatch();
var tempLstSearch = lstGetResult;
var txtCode = txtCode.Text.Trim();
var ts1 = stopWatch.Elapsed;
if(txtCode.Length > 0)
   tempLstSearch = tempLstSearch.Where(i=> (
                                             (i.CODE.IndexOf(txtCode,System.StringComparison.CurrentCultureIgnoreCase) >= 0) 
                                             (i.NAME.IndexOf(txtCode,System.StringComparison.CurrentCultureIgnoreCase) >= 0)
   var ts2 = stopWatch.Elapsed;
var search =  From s In tempLstSearch 
              Select New SPGet_Result With {.CODE = s.CODE, .NAME = s.NAME};
varts3 = stopWatch.Elapsed;
var lstSearch = search.ToList()
var ts4 = stopWatch.Elapsed;
URVISHSUTHAR 4-Jan-13 5:09am
thanks for hint :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

lstSearch = (From s In lstGetResult
      Where (s.CODE.IndexOf(txtCode.Text.Trim(), StringComparison.OrdinalIgnoreCase) >= 0 OrElse
      s.NAME.IndexOf(txtCode.Text.Trim(), StringComparison.OrdinalIgnoreCase) >= 0)
      Select New SPGet_Result With
         {.CODE = s.CODE,.NAME = s.NAME}).ToList()

Converted using IndexOf and its work more faster

many thanks "katanakensei" for hint :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100