比较两个列表以查看是否需要更新数据库甚至添加到

时间:2019-01-28 13:31:41

标签: c# linq

我有一个简单的类,该类持有一个主键,在我从COM获取数据时,我不知道它的类型。它可以是int或字符串。

我基本上只需要填写我的toUpdateList和toAddList。在下面,此方法运行良好,没有太多记录可玩。但是,现在mongoDBList返回大约65k记录,并且都变得非常慢,并且需要15分钟以上的时间才能解析到UpdateList。

我对C#还是很陌生,所以我可能会错过一些东西。

我基本上只需要将一个列表与另一个列表进行比较,并查看toUpdateList中的RecordRevision是否更高。对于toAddList,这很简单,就好像它不存在一样,需要添加。

感谢您的感激!

class KeyRevision
{
    public dynamic RecordRevision;
    public dynamic PrimaryKey;
}

            List<KeyRevision> potentialUpdateList = new List<KeyRevision>();
            List<KeyRevision> mongoDBList = new List<KeyRevision>();
            List<KeyRevision> toUpdateList = new List<KeyRevision>();
            List<KeyRevision> toAddList = new List<KeyRevision>();

            var sql = env.ExecuteSQL(sqlQuery);

            sql.First();

            // Loop over them and add to array
            do
            {
                if (sql.RecordCount > 0)
                {
                    //Console.WriteLine(sql.GetPropertyValue(primaryKey).ToString() + " : " + sql.RecordRevision);
                    var o = new KeyRevision();
                    o.PrimaryKey = sql.GetPropertyValue(primaryKey);
                    o.RecordRevision = sql.RecordRevision;
                    potentialUpdateList.Add(o);
                }
                sql.Next();
            } while (!sql.EOF);

            // Ask mongo for docs
            var docs = collection1.Find(_ => true).Project("{RecordRevision: 1}").ToList();

            // Get them into our type
            mongoDBList = docs.ConvertAll(x => new KeyRevision()
            {
                PrimaryKey = x.GetValue("_id"),
                RecordRevision = x.GetValue("RecordRevision")
            });

            // Finds which records we need to update
            toUpdateList = potentialUpdateList.Where(x =>
                mongoDBList.Any(y => y.PrimaryKey == x.PrimaryKey && y.RecordRevision < x.RecordRevision)).ToList();

            // Finds the records we need to add
            toAddList = potentialUpdateList.Where(x =>
                mongoDBList.FindIndex(y => y.PrimaryKey == x.PrimaryKey) < 0).ToList();

            Console.WriteLine($"{toUpdateList.Count} need to be updated");
            Console.WriteLine($"{toAddList.Count} need to be updated");

0 个答案:

没有答案