简体   繁体   English

有没有办法从ObservableCollection过滤非零数据?

[英]Is there a way to filter non zero data from an ObservableCollection?

is there a way to filter and group data from an ObservableCollection that has values greater than zero? 有没有办法从值大于零的ObservableCollection筛选和分组数据?

I have an excel file that load into an ObservableCollection but within the file could be some values with zero, and with this criteria, I need to run a query into SQL to get some data, but with these zero codes when I run the query doesn't return anything. 我有一个加载到ObservableCollection中的excel文件,但该文件中的某些值可能为零,并且使用此条件,我需要对SQL运行查询以获取一些数据,但是当我运行查询时,使用这些零代码不会什么都不会退

Example

在此处输入图片说明

I need to build a query for each of this combinations but there are more than 90k records in my ObservableCollection I'm looking for a clever solution to group all the records with the same pattern (In this case row 3 and 4 for example) does not need to have the same values only the same pattern in order to run one single large query with all of these codes. 我需要为每种组合构建一个查询,但是我的ObservableCollection中有超过90k条记录,我正在寻找一个聪明的解决方案,以相同的模式对所有记录进行分组(例如,在本例中,第3和第4行)为了使用所有这些代码运行一个大型查询,不需要仅具有相同模式的相同值。

Currently, I'm doing it in this way: 目前,我正在以这种方式进行操作:

foreach (var item in _sourceStructure)
        {
            query = prepareQuery(item);
            RunQuery(query);
            if (errorMessage.Length > 1)
            {
                Console.WriteLine(errorMessage);
            }
            query = "";
        }

And in my preparQuery method, I have something like this 在我的preparQuery方法中,我有这样的东西

query = @"SELECT Column1, Column2, Column3, Column4 FROM SomeTable WHERE "

if (!string.IsNullOrEmpty(item.Column1.ToString()) && item.Column1.ToString() != "0")
                {
                    query = query + "Column1= " + "'" + item.Column1.ToString() + "'" + " and ";
                }
                if (!string.IsNullOrEmpty(item.Column2.ToString()) && item.Column2.ToString() != "0")
                {
                    query = query + "Column2= " + "'" + item.Column2.ToString() + "'" + " and ";
                }

This works "fine" but takes a lot of time to complete. 这项工作“很好”,但是要花很多时间才能完成。 If you find a better solution I can try it, I'm just looking for some advice. 如果您找到更好的解决方案,可以尝试一下,我只是在寻找一些建议。

Stop calling ToString so many times. 停止调用ToString多次。 You should do it once and store the value. 您应该执行一次并存储值。 And consider using string.Format or StringBuilder. 并考虑使用string.Format或StringBuilder。 Adding is very resource intensive. 添加非常耗费资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM