我要将一个对象添加到集合中,但我想知道如何更新它,如果记录存在且存在norecord。
我正在研究这个问题。
public void AssignProductSetting(CategoryType catType, int catId, int freeCount)
{
this.CustomSettings.Add(new ProductCustomization()
{
CategoryID = catId,
CustomizationType = catType,
DefaultFreeCount = freeCount,
ProductID = this.ProductID
});
}
答案 0 :(得分:2)
if(CustomeSettings == null) CustomerSettings =
new Collection<ProductCustomization>();
var cat = CustomSettings.FirstOrDefault(r=>r.CategoryId == catID &&
r.CustomizationType == catType);
if(cat!=null)
{
cat.DefaultFreeCount = freeCount;
cat.ProductID = this.ProductID;
}
else
{
this.CustomSettings.Add(new ProductCustomization()
{
CategoryID = catId,
CustomizationType = catType,
DefaultFreeCount = freeCount,
ProductID = this.ProductID
});
}
答案 1 :(得分:1)
if(this.CustomSettings.Any(x=>x.CategoryID == catId))
{
//Update
}
else
{
//Add
}