首页 > 解决方案 > 从两个列表中过滤掉重复项,如果重复则更改对象属性值

问题描述

我有两个列表 AuthorList 和 AuthorList2。目前我正在使用带有简单 IEqualityComparer 类的联合。我希望有一个没有来自 AuthorList 和 AuthorList2 的重复项的结果列表,如果这些列表中有任何重复项,则需要将它们从列表中删除,并且需要将重复项的 Author 类 Assigned 属性设置为 true。

来自两个 AuthorLists 的现有信息:

产品 ID 和分配

结果列表:

产品 ID 和分配

逻辑需要过滤掉重复项,如果这两个列表具有相同的元素,则更改false -> true

namespace HelloWorld
{
     class Hello
      {
        static void Main()
        {

            List<Author> AuthorList = new List<Author>
            {
                new Author(1, false),
                new Author(2, false),
                new Author(3, false)
            };


            List<Author> AuthorList2 = new List<Author>
            {
                new Author(1, false)
            };

            var compareById = new AuthorComparer(false);

            var result = AuthorList.Union(AuthorList2, compareById);

            foreach (var item in result)
            {
                Console.WriteLine("Result: {0},{1}", item.ProductId, item.Assigned);
            }

            Console.ReadKey();
        }

        public class AuthorComparer : IEqualityComparer<Author>
        {
            private bool m_withValue;

            public AuthorComparer(bool withValue)
            {
                m_withValue = withValue;
            }

            public bool Equals(Author x, Author y)
            {
                return (x.ProductId == y.ProductId);
            }

            public int GetHashCode(Author x)
            {
                return x.ProductId.GetHashCode();
            }
        }

        public class Author
        {
            private int productId;
            private bool assigned;

            public Author(int productId, bool assigned)
            {
                this.productId = productId;
                this.assigned = assigned;
            }

            public int ProductId
            {
                get { return productId; }
                set { productId = value; }
            }

            public bool Assigned
            {
                get { return assigned; }
                set { assigned = value; }
            }
        }
      }
    }

标签: c#.net

解决方案


您正在寻找的代码是这样的:

AuthorList.ForEach(a => a.Assigned = AuthorList2.Exists(b => b.ProductId == a.ProductId));

你根本不需要IEqualityComparer

完整的工作代码:

namespace HelloWorld
{
    class Hello
    {
        static void Main()
        {

            List<Author> AuthorList = new List<Author>
            {
                new Author(1, false),
                new Author(2, false),
                new Author(3, false)
            };


            List<Author> AuthorList2 = new List<Author>
            {
                new Author(1, false)
            };

            AuthorList.ForEach(a => a.Assigned = AuthorList2.Exists(b => b.ProductId == a.ProductId));

            foreach (var item in AuthorList)
            {
                Console.WriteLine("Result: {0},{1}", item.ProductId, item.Assigned);
            }

            Console.ReadKey();
        }

        public class Author
        {
            public Author(int productId, bool assigned)
            {
                this.ProductId = productId;
                this.Assigned = assigned;
            }

            public int ProductId { get; set; }

            public bool Assigned { get; set; }
        }
    }
}

推荐阅读