为了账号安全,请及时绑定邮箱和手机立即绑定

从两个列表中过滤掉重复项并在重复时更改对象属性值

从两个列表中过滤掉重复项并在重复时更改对象属性值

C#
海绵宝宝撒 2021-06-03 03:15:40
我有两个列表 AuthorList 和 AuthorList2。目前我正在使用带有简单 IEqualityComparer 类的 union。我希望有一个结果列表,并且 AuthorList 和 AuthorList2 中没有任何重复项,如果这些列表中有任何重复项,则需要从列表中删除它们,并且需要为重复项将 Author 类的 Assigned 属性设置为 true。来自两个 AuthorList 的现有信息:产品 ID 和已分配1、假的2、假的3、假的1、假的结果列表:产品 ID 和已分配1、真2、假的3、假的该逻辑需要过滤掉重复项,如果这两个列表具有相同的元素,请更改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; }            }        }      }    }
查看完整描述

3 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

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


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; }

        }

    }

}


查看完整回答
反对 回复 2021-06-05
  • 3 回答
  • 0 关注
  • 200 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信