C#比较特定属性的两个大型项目列表 [英] C# comparing two large lists of items by a specific property

查看:69
本文介绍了C#比较特定属性的两个大型项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个大的项目列表,它们的类看起来像这样(两个列表的类型相同):

I have two large lists of items whos class look like this (both lists are of same type):

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提取的项目;

The old list contains items from database and the new list represents items fetched from API;

两个列表都非常大,每个列表中包含10000+个项目(共20000个)

Both lists can be very large with 10000+ items in each (20000 total)

我需要将 newList 中的项目与oldList"中的项目进行比较,看看哪些项目具有相同的 itemID 值,具有不同的QuantitySold"值,而那些具有不同QuantitySold"值的项目应该存储在第三个列表称为"differentQuantityItems".

I need to compare items from newList against the items from "oldList" and see which items that have same itemID value, are of different "QuantitySold" value, and those that are of different "QuantitySold" value should be stored in third list called "differentQuantityItems".

我可以简单地做一遍foreach列表并比较值,但是由于两个列表都很大,因此使用double foreach循环的性能非常糟糕,我做不到...

I could just simply do double foreach list and compare values but since both of the lists are large the performance with double foreach loop is terrible and I can't do it...

有人可以帮我吗?

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

@YamamotoTetsua I'm already using a IEqualityComparer to get the desired result, however it doesn't gives the results that I'm expecting. Here is why...I have a first IEqualityComparer which looks like this:

 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 的项目,这些项目不存在于我的数据库中,如下所示:

The usage of this IEqualityComparer basically gives me items from newList that are not present in my database like following:

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

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

Now in this list I will have the list of items which are new from API and are not present in my DB...

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

Second IEqualityComparer is based on the different QuantitySold from old and new list:

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:

The issue with these two equality comparers is that first one will for example return these itemID's that are missing:

123124124

123124421

512095902

它们确实从我的oldList中丢失了...但是,第二个IEQualityComparer也将这些项目作为differentQuantity项目返回,它们确实存在,但是在oldList中不存在.因此,不应将它们包括在其中第二个列表.

And they indeed are missing from my oldList... However the second IEQualityComparer will also return these items as differentQuantity items, they indeed are, but the aren't present in the oldList.. So they shouldn't be included in the second list.

推荐答案

这是LINQ 这将返回所有新项目,这些新项目具有对应的旧项目,并具有不同的QuantitySold.如果您还希望包括新项目而没有相应的旧项目,则使用左外部联接:

This will return all new items which have corresponding old item with different QuantitySold. If you want to also include the new items without corresponding old item, then use 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或任何其他属性.

In both cases, join operator is used to quickly correlate the items with the same ItemID. Then you can compare QuantitySold or any other properties.

这篇关于C#比较特定属性的两个大型项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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