我使用 c# 3.5 和 linq 编写了一个简单的程序。
我有课
public class Product
{
public Product()
{
}
public int RoleId { get; set; }
public int ObjectId { get; set; }
public bool Read { get; set; }
public override bool Equals(object obj)
{
return Equals((Product) obj);
}
public bool Equals(Product other)
{
return ObjectId == other.ObjectId && Read == other.Read;
}
}
我正在尝试比较列表。
List<Product> products = new List<Product>()
{
new Product { RoleId = 1, ObjectId = 2, Read = false },
new Product { RoleId = 2, ObjectId = 1, Read = false },
new Product { RoleId = 1, ObjectId = 1, Read = true }
};
var groupedCustomerList = products.GroupBy(u => u.RoleId)
.Select(grp => grp.ToList()).ToList();
var firstGroup = groupedCustomerList.ElementAt(0);
List<Product> productsListSearch = new List<Product>()
{
new Product {ObjectId = 1, Read = true },
new Product {ObjectId = 2, Read = false }
};
var result= productsListSearch.SequenceEqual(firstGroup);
为什么结果不正确?我需要对物品进行分类吗?