收集数据我需要从数据库中删除到列表< DataRow> [英] Gathering Data I need To Remove From Database Into A List<DataRow>

查看:55
本文介绍了收集数据我需要从数据库中删除到列表< DataRow>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个List< DataRow> - 一个包含我需要保留的DataRows,另一个包含更大的DataRows集。列表< DataRow> dataToKeep包含我需要保留的行,List< DataRow> dataToRemove包含
一大组值。我需要更新dataToRemove List< DataRow>包含
dataToKeep中 not 的所有值。
那么我该如何去除dataToRemove中同样位于dataToKeep中的值? 

I currently have two List<DataRow> - one that contains DataRows I need to keep, and the other contains a much larger set of DataRows. The List<DataRow> dataToKeep contains the rows I need to keep, and the List<DataRow> dataToRemove contains a large set of values. I need to update the dataToRemove List<DataRow> to contain all values that are not in dataToKeep. So how can I go about removing the values in dataToRemove that are also in dataToKeep? 

这是代码 -

            List<DataRow> dataToKeep = new List<DataRow>();
            List<DataRow> dataToRemove = new List<DataRow>(); 

            foreach (DataRow r in table.Rows)
            {
                dataToKeep.Add(r);
            }

            foreach (DataRow r in allData.Rows)
            {
                dataToRemove.Add(r);
            }

            dataToRemove.RemoveAll(item => dataToKeep.Contains(item));

            foreach (DataRow r in dataToRemove)
            {
                foreach (var item in r.ItemArray)
                {
                    Console.WriteLine(" | " + item);
                }
                Console.WriteLine();
            }

我认为问题行是 

I think the issue line is 

dataToRemove.RemoveAll(item => dataToKeep.Contains(item));

此外,我想到的可能是因为我无法比较Rows是否相等,但有没有一种有效的方法比较DataRows中的值是否相等?我不知道该怎么做,但是有没有办法比较DataRows的值
而不是DataRows本身
有效吗?
像.Equals()的版本,但是对于DataRows而不是==。 

Also, something I thought about is that maybe it is because I cannot compare the Rows to be equal, but is there an efficient way to compare the values within the DataRows to be equal? I didn't know how to do that, but is there a way to compare the values of DataRows rather than the DataRows themselves efficiently? Like a version of .Equals() but for DataRows instead of ==. 

我让它在一夜之间运行(非常大的数据集)并且它从未被传递过线。我怎样才能更有效地做到这一点/它实际上有效? 

I left it to run overnight (very large set of data) and it never got passed that line. How can I do this more efficiently / so it actually works? 

推荐答案

你说得对,这里有一个问题:

You are right in that there is a problem here:

项目 => dataToKeep 包含 item

"包含""应用于DataRow对象的方法将调用Equals,并且由于DataRow类没有Equals的覆盖,因此它会调用System.Object中的Equals方法,而System.Object又调用ReferenceEquals。后者比较两个对象的引用(内存中的地址)
。总之,结果是如果两个DataRows是对完全相同的对象的两个引用,则它们被认为是相等的。两个不同的DataRow对象在字段的所有
中包含相同的值将不被视为相等。

The "Contains" method applied to a DataRow object will invoke Equals, and since the DataRow class does not have an override of Equals this in turn invokes the Equals method in System.Object, which in turn invokes ReferenceEquals. The latter compares the references (addresses in memory) of the two objects. In summary, the result is that two DataRows are only considered to be equal if they are two references to the exact same object. Two different DataRow objects that contain the same values in all of the fields would not be considered to be equal.

不幸的是,没有"高效"修复此问题的方法您需要遍历每个数据行中的所有字段,逐个比较它们。

Unfortunately, there is no "efficient" way to fix this You would need to loop through all the fields in each datarow comparing them one by one.

如果您像对待数据库中的表一样处理数据表,那么它将一个字段指定为主键,并且您将生成该字段的内容,使其以单义方式表示行,这意味着如果
两行具有相同的主键,那么我们考虑两行都相等。使用这种组织,比较两行意味着只需比较它们的主键,因此您可以进行快速有效的比较,特别是如果主要的
键字段已编入索引。但是,如果您没有以这种方式组织数据并且所有字段都是完全任意的并且您需要将它们全部进行比较,那么您将需要一个额外的循环来逐个比较字段,这不会是$如果您有大量数据,b $ b会很快。

If you treat the datatable like you would treat a table in a database, then it would have one field designated as a primary key, and you would generate the content of that field in such a way that it represented the row in an univocal way, meaning that if two rows have the same primary key then we consider that both rows are equal. With this kind of organization, comparing two rows would mean just comparing their primary keys, and therefore you could do a quick and efficient comparison, especially if the primary key field was indexed. But if you don't have your data organized in such a way and all your fields are completely arbitrary and you need to compare them all, you will need an additional loop to compare the fields one by one, which is not going to be quick if you have large amounts of data.


这篇关于收集数据我需要从数据库中删除到列表&lt; DataRow&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆