C#比较特定属性的两个大项目列表

问题描述 投票:2回答:4

我有两个大类的项目列表,这两个列表是相同类型的:

public class Items
{
 public string ItemID { get; set; }
 public int QuantitySold { get; set; }
}


var oldList = new List<Items>(); // oldList

var newList = new List<Items>(); // new list

旧列表包含来自数据库的项目,新列表表示从API获取的项目;

两个列表都非常大,每个都有10000多个项目(总共20000个)

我需要将newList中的项目与“oldList”中的项目进行比较,并查看哪些项目具有相同的itemID值,具有不同的“QuantitySold”值,而那些具有不同“QuantitySold”值的项目应存储在名为“oldSold”的第三个列表中differentQuantityItems”。

我可以简单地做双foreach列表并比较值,但由于两个列表都很大,双foreach循环的性能很糟糕,我做不到...

有人可以帮我解决这个问题吗?

@YamamotoTetsua我已经使用IEqualityComparer来获得所需的结果,但是它没有给出我期望的结果。这就是为什么...我有一个第一个IEqualityComparer,它看起来像这样:

 public class MissingItemComparer : IEqualityComparer<SearchedUserItems>
    {
        public static readonly IEqualityComparer<SearchedUserItems> Instance = new MissingItemComparer();

        public bool Equals(SearchedUserItems x, SearchedUserItems y)
        {
            return x.ItemID == y.ItemID;
        }

        public int GetHashCode(SearchedUserItems x)
        {
            return x.ItemID.GetHashCode();
        }
    }

这个IEqualityComparer的用法基本上给了我来自newList的项目,这些项目在我的数据库中不存在,如下所示:

var missingItems= newItems.Except(competitor.SearchedUserItems.ToList(), MissingItemComparer.Instance).ToList();

现在在此列表中,我将获得API的新项目列表,但不在我的数据库中...

第二个IEqualityComparer基于来自旧列表和新列表的不同QuantitySold:

public class ItemsComparer : IEqualityComparer<SearchedUserItems>
    {
        public static readonly IEqualityComparer<SearchedUserItems> Instance = new ItemsComparer();
        public bool Equals(SearchedUserItems x, SearchedUserItems y)
        {
            return (x.QuantitySold == y.QuantitySold);
        }
        public int GetHashCode(SearchedUserItems x)
        {
            return x.ItemID.GetHashCode();
        }
    }

用法示例:

var differentQuantityItems = newItems.Except(competitor.SearchedUserItems.ToList(), ItemsComparer.Instance).ToList();

这两个相等比较器的问题是,例如,第一个将返回缺少的itemID:

123124124

123124421

512095902

并且它们确实从我的oldList中丢失了......但是第二个IEQualityComparer也会将这些项目作为不同的数量项返回,它们确实是,但是它们不在oldList中。所以它们不应该包含在第二个列表中。

c# list performance linq c#-4.0
4个回答
6
投票

这是LINQ Join的完美候选者:

var differentQuantityItems =
    (from newItem in newList
     join oldItem in oldList on newItem.ItemID equals oldItem.ItemID
     where newItem.QuantitySold != oldItem.QuantitySold
     select newItem).ToList();

这将返回具有不同QuantitySold的相应旧项目的所有新项目。如果您还要包含没有相应旧项目的新项目,请使用left outer join

var differentQuantityItems =
    (from newItem in newList
     join oldItem in oldList on newItem.ItemID equals oldItem.ItemID into oldItems
     from oldItem in oldItems.DefaultIfEmpty()
     where oldItem == null || newItem.QuantitySold != oldItem.QuantitySold
     select newItem).ToList();

在这两种情况下,join运算符用于快速将项目与相同的ItemID相关联。然后,您可以比较QuantitySold或任何其他属性。


1
投票

这段代码将在不到一秒的时间内运行,即使根本没有匹配(如果一切都匹配,也会少于一秒)。

它将返回两个列表中存在的所有项目(即相同的ItemID)但具有不同的QuantitySold

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp5
{
    class Program
    {
        public class Items
        {
            public string ItemID { get; set; }
            public int QuantitySold { get; set; }
        }

        static void Main(string[] args)
        {
            // Sample data
            var oldList = new List<Items>();
            oldList.AddRange(Enumerable.Range(0, 20000).Select(z => new Items() { ItemID = z.ToString(), QuantitySold = 4 }));

            var newList = new List<Items>();
            newList.AddRange(Enumerable.Range(0, 20000).Select(z => new Items() { ItemID = z.ToString(), QuantitySold = 5 }));

            var results = oldList.Join(newList,
                                            left => left.ItemID,
                                            right => right.ItemID,
                                            (left, right) => new { left, right })
                                .Where(z => z.left.QuantitySold != z.right.QuantitySold).Select(z => z.left);

            Console.WriteLine(results.Count());
            Console.ReadLine();
        }
    }
}

使用z.left意味着只返回其中一个项目 - 如果你想要旧的和新的,而是使用:

var results = oldList.Join(newList,
                                left => left.ItemID,
                                right => right.ItemID,
                                (left, right) => new { left, right })
                    .Where(z => z.left.QuantitySold != z.right.QuantitySold)
                    .Select(z => new[] { z.left, z.right })
                    .SelectMany(z => z);

1
投票

从大O复杂的角度来看,仅比较嵌套for循环中的列表将在O(n * m)的类中,n是DB中列表的大小,m是列表的大小从API获取。

你可以做些什么来提高你的性能是对两个列表进行排序,这将花费O(n log(n)+ m log(m)),然后你可以在O(n + m)中找到新项目。因此,算法的总体复杂度将在O(n log(n)+ m log(m))的类中。

Here's an idea将花费时间,将二次解与超线性解相比较。


0
投票

您可以考虑使用自定义编写的IEqualityComparer的Except子句,如下所示

var oldList = new List<Item>(); // oldList
var newList = new List<Item>(); // new list
var distinctList = newList.Except(oldList,new ItemEqualityComparer()).ToList();



class ItemEqualityComparer : IEqualityComparer<Item>
        {
            public bool Equals(Item i1, Item i2)
            {
                if (i1.ItemID == i2.ItemID && i1.QuantitySold != i2.QuantitySold)
                    return false;
                return true;
            }

            public int GetHashCode(Item item)
            {
                return item.ItemID.GetHashCode();
            }
        }

public class Item
        {
            public string ItemID { get; set; }
            public int QuantitySold { get; set; }
        }
© www.soinside.com 2019 - 2024. All rights reserved.