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; }
}
}
}
- 3 回答
- 0 关注
- 215 浏览
添加回答
举报