techknackblogs
techknackblogs

Reputation: 131

Linq performance issue with huge record set

I am writing a LINQ query and its performance is much slower than fetching from DB. Can you please give some idea how can I increase the performance.

cmbMedicines.DataSource = Lookup.Medicines
       .Where(d => d.DosageForm.Equals(cmbType.SelectedValue.ToString()))
       .AsParallel().ToList();

Here cmbMedicines is a Combobox and Lookup.Medicines is the List of Medicines and it has around 100K records.

Upvotes: 0

Views: 1171

Answers (4)

Sameer
Sameer

Reputation: 3173

Try this :

var selectedVal = cmbType.SelectedValue.ToString();
cmbMedicines.DataSource = Lookup.Medicines.AsParallel()
      .Where(d => d.DosageForm.Equals(selectedVal)).ToList();

Upvotes: 2

Alice
Alice

Reputation: 1265

I think that the Combobox data binding process also reduce the performance. If it possible for you, you should use other control instead of the Combobox to handle a big data.

I assume that your users should know which madicine they want to select, so they could put a part of medicine name in search box. This just a choice, you may use TextBox (names txtMedicines in my sample code) with AutoComplete to search binding data when user enter some word to search in the list.

This is a sample code

private void InitializeMedicinesAutoComplete()
{
   var searchMed = Lookup.Medicines
       .Where(d => d.DosageForm.Equals(cmbType.SelectedValue.ToString())).ToList(); 

   var source = new AutoCompleteStringCollection();
   foreach (var med in searchMed)
   {
      // **DisplayMemberText mean any field that you want to display in searching list
      source.Add(med.DisplayMemberText); 
   }
   txtMedicines.AutoCompleteMode = AutoCompleteMode.Suggest;
   txtMedicines.AutoCompleteSource = AutoCompleteSource.CustomSource;
   txtMedicines.AutoCompleteCustomSource = source;
}

private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
{
   InitializeMedicinesAutoComplete();
}

I hope this will help.

Upvotes: 0

Dweeberly
Dweeberly

Reputation: 4777

More info would be useful. One thing I wonder about is the "Equals" call. First is DosageForm a string? I wonder if you created a string outside the Linq statement if it would be faster. For example (assuming DosageForm is string):

var val = cmbType.SelectedValue.ToString();
cmbMedicines.DataSource = Lookup.Medicines
            .Where(d => d.DosageForm == val))
            .ToList();

Also I think that DataSource will accept and IEnumerable, so if you drop the ".ToList()" you might save a lot of time (if the query returns a lost of stuff).

SQL servers are highly optimized for this sort of thing so depending on the overhead required to transmit the data it might be difficult to beat.

Upvotes: 1

Noctis
Noctis

Reputation: 11783

As much as I love linq, it's not the solution to all humankind problems ... Why would you like to have 100K objects in memory so you can then linq them, if you can have them in a database, and just query the database to get the results?

Use the database as it was supposed to be used, and use linq as it was supposed to be used ...

Upvotes: 0

Related Questions