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

我有两个列表 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; }

            }


        }

      }

    }


海绵宝宝撒
浏览 201回答 3
3回答

拉丁的传说

您正在寻找的代码是这样的:AuthorList.ForEach(a => a.Assigned = AuthorList2.Exists(b => b.ProductId == a.ProductId));你根本不需要IEqualityComparer。完整的工作代码:namespace HelloWorld{&nbsp; &nbsp; class Hello&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Author> AuthorList = new List<Author>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Author(1, false),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Author(2, false),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Author(3, false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Author> AuthorList2 = new List<Author>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Author(1, false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AuthorList.ForEach(a => a.Assigned = AuthorList2.Exists(b => b.ProductId == a.ProductId));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in AuthorList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Result: {0},{1}", item.ProductId, item.Assigned);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public class Author&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Author(int productId, bool assigned)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.ProductId = productId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Assigned = assigned;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int ProductId { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public bool Assigned { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP