C# 数据集中的唯一且不同的字段

发布于 2024-12-14 13:35:46 字数 3650 浏览 0 评论 0原文

有没有更好的方法来实现这个功能?尽管我正在后台工作人员内部进行实际查询,但在查询发生时应用程序中会有一点暂停,所以想知道是否有更快的方法来执行此操作。

本质上,查询正在检查数据集以获取特定列的唯一结果,以使用这些项目填充组合框。这使我们不必将任何选项硬编码到过滤器中,并且如果我们在服务器上为这些值添加项目,它们将在下次数据集自动更新时添加。

我只是不确定我正在使用的方法是否可以以更短、更快的格式完成:

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);
   }

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);
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-12-21 13:35:46

您实际上并没有在后台线程中完成这项工作 - 您只是在准备它。调用 Distinct() 仅构建一个 IEnumerable,当您对其进行迭代时,它将产生不同的项。在“完成”方法之前,您不会对其进行迭代,该方法将返回到 UI 线程中。

您完全有可能通过强制在后台线程中执行查询来消除延迟,如下所示:(

 queryStatus = demoCriteria.AsEnumerable()
                           .Select(row => row.Field<string>("ows_Status"))
                           .Distinct()
                           .ToList(); // Note this call!

显然,其他值也是如此)。

You're not actually doing the work in the background thread - you're just preparing it. Calling Distinct() only builds an IEnumerable<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:

 queryStatus = demoCriteria.AsEnumerable()
                           .Select(row => row.Field<string>("ows_Status"))
                           .Distinct()
                           .ToList(); // Note this call!

(and the same for the other values, obviously).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文