Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ VB.NET
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 Smile | :)
Posted 3-Jan-13 20:31pm
Comments
Suvabrata Roy at 4-Jan-13 2:35am
   
lstGetResult how many entity's are there
URVISHSUTHAR at 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
good
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();
stopWatch.Start();
 
var tempLstSearch = lstGetResult;
var txtCode = txtCode.Text.Trim();
 
stopWatch.Stop();
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)
                                            ));
   stopWatch.Stop();
   var ts2 = stopWatch.Elapsed;
}
 
var search =  From s In tempLstSearch 
              Select New SPGet_Result With {.CODE = s.CODE, .NAME = s.NAME};
 
stopWatch.Stop();
varts3 = stopWatch.Elapsed;
 
var lstSearch = search.ToList()
 
stopWatch.Stop();
var ts4 = stopWatch.Elapsed;
 
  Permalink  
v2
Comments
URVISHSUTHAR at 4-Jan-13 5:09am
   
thanks for hint :)
Rate this: bad
good
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 Smile | :)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 231
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
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