亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從兩個列表中過濾掉重復項并在重復時更改對象屬性值

從兩個列表中過濾掉重復項并在重復時更改對象屬性值

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 關注
  • 240 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號