sharath reddy
sharath reddy

Reputation: 93

Improve performance in linq query

We have a linq query as shown below to display items first it should display records matching with the text and next with if the text contains in the string.But it is causing lot of performance issue. Could any one pls help how to improve it.

Dim result1 = From entry As IAutoCompleteEntry In oldList
             Where entry.ToString.ToUpper.Contains(Me.Text.ToUpper())
             Order By entry.ToString.IndexOf(Me.Text.ToUpper()), entry.ToString.ToUpper.StartsWith(Me.Text.ToUpper())
              Descending
             Select entry

Upvotes: 1

Views: 149

Answers (2)

Dylan Corriveau
Dylan Corriveau

Reputation: 2557

First, I'm not sure what the significance of the .StartWith has. Since you are already getting the IndexOf, you would already know the answer of StartsWith (it should be 0).

Next, you really shouldn't be using ToUpper (as @RobertMcKee mentioned), instead you should be using case insensitive comparisons. In that case, you shouldn't need the .ToUpper's anymore either...

Finally, I was actually going to say use contains for your second statement, but you probably don't even need it. You could just sort descending based on your entry variable. Here is an updated query I wrote up:

dim result1 = From entry As IAutoCompleteEntry In oldList
              Where (entry.IndexOf(Me.Text, StringComparison.OrdinalIgnoreCase) <> -1)
              Order By entry Descending
              Select entry

Upvotes: 1

SKG
SKG

Reputation: 152

You are calling ToUpper() at several places. Is it possible to get your list/array to have a ToUpper() before you get into this linq query? I think you can also have another column for entry.ToString.IndexOf(Me.Text.ToUpper()), entry.ToString.ToUpper.StartsWith(Me.Text.ToUpper()) before getting to this linq query. It might improve on performance....

Upvotes: 1

Related Questions