Is there a better way to do this function? Even though i’m doing the actual query inside of a background workder, there is a bit of a slight pause in the application as the query is taking place, so was wondering if there was a faster way of doing this.
Essentially, the queries are checking the dataset for unique results from specific columns, to populate a comboBox with those items. This lets us not have to hard code any options into the filters, and if we add items on the server for those values, they are added when the next time the dataset is updated automatically.
I’m just not sure if the methods i’m using could be done in a shorter, quicker format:
private IEnumerable<string> queryStatus;
private IEnumerable<string> queryPriority;
private IEnumerable<string> queryCompany;
private IEnumerable<string> queryCategory;
private void filterBuilder_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString());
DataTable demoCriteria = Ds.Tables[1];
queryStatus = (demoCriteria.AsEnumerable().Select(row => row.Field<string>("ows_Status"))).Distinct();
queryPriority = (demoCriteria.AsEnumerable().Select(row => row.Field<string>("ows_Priority"))).Distinct();
queryCompany = (demoCriteria.AsEnumerable().Select(row => row.Field<string>("ows_Company"))).Distinct();
queryCategory = (demoCriteria.AsEnumerable().Select(row => row.Field<string>("ows_Category"))).Distinct();
}
private void filterBuilder_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
foreach (string row in queryStatus)
{
if (!viewFilter_Status.Items.Contains(row))
viewFilter_Status.Items.Add(new ComboBoxItem(row,
row));
if (!editStatus.Items.Contains(row))
editStatus.Items.Add(new ComboBoxItem(row,
row));
if (!newStatus.Items.Contains(row))
newStatus.Items.Add(new ComboBoxItem(row,
row));
}
foreach (string row in queryPriority)
{
if (!viewFilter_Priority.Items.Contains(row))
viewFilter_Priority.Items.Add(new ComboBoxItem(row,
row));
if (!editPriority.Items.Contains(row))
editPriority.Items.Add(new ComboBoxItem(row,
row));
if (!newPriority.Items.Contains(row))
newPriority.Items.Add(new ComboBoxItem(row,
row));
}
foreach (string row in queryCompany)
{
if (!viewFilter_Company.Items.Contains(row))
viewFilter_Company.Items.Add(new ComboBoxItem(row,
row));
if (!editCompany.Items.Contains(row))
editCompany.Items.Add(new ComboBoxItem(row,
row));
if (!newCompany.Items.Contains(row))
newCompany.Items.Add(new ComboBoxItem(row,
row));
}
foreach (string row in queryCategory)
{
if (!viewFilter_Product.Items.Contains(row))
viewFilter_Product.Items.Add(new ComboBoxItem(row,
row));
if (!editProduct.Items.Contains(row))
editProduct.Items.Add(new ComboBoxItem(row,
row));
if (!newProduct.Items.Contains(row))
newProduct.Items.Add(new ComboBoxItem(row,
row));
}
MainFormCallbacks.EnableISTab(true);
}
You’re not actually doing the work in the background thread – you’re just preparing it. Calling
Distinct()only builds anIEnumerable<T>which will yield distinct items when you iterate over it. You’re not iterating over it until the “completed” method, which will be back in the UI thread.It’s entirely possible that you can get rid of the delay by forcing the query to be executed in the background thread, like this:
(and the same for the other values, obviously).