【问题标题】:Compare 2 lists with different values比较具有不同值的 2 个列表
【发布时间】:2016-06-01 13:36:29
【问题描述】:

我想比较两个包含不同值但只有一个唯一属性的对象列表RefCode

示例输入:

列表 1

产品(CurrentName =“GenP”,RefCode =“MM01”,年份 = 2015)

产品(CurrentName =“GenS”,RefCode =“MM02”,年份 = 2015)

产品(CurrentName =“GenK”,RefCode =“MM03”,年份 = 2014)

清单 2

产品(CurrentName =“GenP2”,RefCode =“MM01”,年份 = 2016)

产品(CurrentName =“GenS3”,RefCode =“MM02”,年份 = 2016)

产品(CurrentName =“GenKF”,RefCode =“MM15”,年份 = 2016)

结果应该是

产品(CurrentName =“GenP”,RefCode =“MM01”,年份 = 2015)

产品(CurrentName =“GenS”,RefCode =“MM02”,年份 = 2015)

因为这些项目可以在基于 RefCode 的 List 2 中找到 使用Enumerable.Except 不起作用,当我比较两个列表时,我得到了 0 条记录。

有什么想法吗? 谢谢

【问题讨论】:

标签: c# linq


【解决方案1】:

您可以使用 LINQ WhereAny 执行以下操作:

var result =
    list1
    .Where(x => list2.Any(y => x.RefCode == y.RefCode))
    .ToList();

出于性能原因,您可以像这样使用HashSet

//Create a hashset that contains all RefCodes from list2
var hashset = new HashSet<string>(list2.Select(x => x.RefCode));

var result =
    list1
    .Where(x => hashset.Contains(x.RefCode))
    .ToList();

【讨论】:

    【解决方案2】:

    您可以使用简单的 LINQ 查询:

    list1.Where(x => list2.Any(v => v.RefCode == x.RefCode));
    

    【讨论】:

      【解决方案3】:

      另一种选择:

      List<Product> result = products1.Join(products2, p1 => p1.RefCode, p2 => p2.RefCode, (p1, p2) => p1).ToList();
      

      【讨论】:

        【解决方案4】:

        您需要使用Intersect 而不是Distinct,但由于您只处理一个字段,您需要使用EqualityComparer

        class Product
        {
            public Product(string currentName, string refCode, int year)
            {
                CurrentName = currentName;
                RefCode = refCode;
                Year = year;
            }
        
            public string CurrentName { get; }
            public string RefCode { get; }
            public int Year { get;}
        }
        
        class ProductEqualityComparer : EqualityComparer<Product>
        {
            public override bool Equals(Product x, Product y)
            {
                return x.RefCode.Equals(y.RefCode);
            }
        
            public override int GetHashCode(Product obj)
            {
                return obj.RefCode.GetHashCode();
            }
        }
        
        [TestClass]
        public class CompareEntriesFixture 
        {
        
            [TestMethod]
            public void CompareEntries()
            {
                var list1 = new List<Product>
                {
                    new Product("GenP", "MMO1", 2015),
                    new Product("GenS", "MMO2", 2015),
                    new Product("GenK", "MMO3", 2014),
                };
        
                var list2 = new List<Product>
                {
                    new Product("GenP2", "MMO1", 2016),
                    new Product("GenS3", "MMO2", 2016),
                    new Product("GenKF", "MM15", 2016),
                };
        
                var expected = new List<Product>
                {
                    new Product("GenP", "MMO1", 2015),
                    new Product("GenS", "MMO2", 2015)
                };
        
                var common = list1.Intersect(list2, new ProductEqualityComparer()).ToList();
        
                CollectionAssert.AreEqual(expected, common, new ProductComparer());
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-07
          • 2021-01-14
          • 2021-10-18
          相关资源
          最近更新 更多