Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Can anyone please suggest me why my linq query taking time

SQL
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
Comments
Suvabrata Roy 4-Jan-13 2:35am    
lstGetResult how many entity's are there
URVISH_SUTHAR1 4-Jan-13 2:59am    
single entity with 5 to 6 thousand rows and I wanted to filter it on Textbox changed event

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.


C#
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;
 
Share this answer
 
v2
Comments
URVISH_SUTHAR1 4-Jan-13 5:09am    
thanks for hint :)
SQL
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 :)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900