C#字典计算组值之和
我目前正在用C#编写一个应用程序。想象一个有收银台的店面。我有一个字典结构,其中一个对象作为键,一个int对象计数器作为值 结构如下所示:C#字典计算组值之和,c#,.net,dictionary,C#,.net,Dictionary,我目前正在用C#编写一个应用程序。想象一个有收银台的店面。我有一个字典结构,其中一个对象作为键,一个int对象计数器作为值 结构如下所示: Dictionary<myObject, int> items. public class myObject { string ItemId { get; set; } Discount Discount { get; set; } } public class Discount { public int Cou
Dictionary<myObject, int> items.
public class myObject
{
string ItemId { get; set; }
Discount Discount { get; set; }
}
public class Discount
{
public int Count { get; set; }
public decimal Price { get; set; }
public IDiscountHandler DiscountHandler => new DiscountHandler();
}
var myObectA = new myObject()
{
ItemId = "A"
};
var discountA = new Discount()
{
Count = 2,
Price = 12 // special price, if 2 myObjects were added to the Dictionary
};
myObjectA.Discount = discountA;
示例myObject可能如下所示:
Dictionary<myObject, int> items.
public class myObject
{
string ItemId { get; set; }
Discount Discount { get; set; }
}
public class Discount
{
public int Count { get; set; }
public decimal Price { get; set; }
public IDiscountHandler DiscountHandler => new DiscountHandler();
}
var myObectA = new myObject()
{
ItemId = "A"
};
var discountA = new Discount()
{
Count = 2,
Price = 12 // special price, if 2 myObjects were added to the Dictionary
};
myObjectA.Discount = discountA;
1) 我填充items字典并将其传递给Handler方法:
private decimal _totalDiscountedValue { get; set; } = 0;
if (!_items.ContainsKey(myObject))
{
_items.Add(myObject, 1);
}
else
{
_items[myObject]++;
}
_totalDiscountedValue += _discountHandler.CalculateDiscount(_items);
2) 在我的处理程序中,一旦计数器规则被完全填充,我尝试将所有折扣值相加。但不幸的是,我在这里挣扎:
public class DiscountHandler : DiscountHandler
{
private decimal _totalDiscount { get; set; } = 0;
public override decimal CalculateDiscount(IDictionary<myObject, int> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
// I'm struggeling here:
// check if Dictionary[i].Dicount.Count = Dictionary.Value
// then _totalDiscount += Dictionary[i].Discount.Price
return _totalDiscount;
}
}
公共类Dispenthandler:Dispenthandler
{
私有十进制_total折扣{get;set;}=0;
公共覆盖十进制计算器(IDictionary项)
{
如果(items==null)抛出新的ArgumentNullException(nameof(items));
//我在这里挣扎:
//检查Dictionary[i].Dicount.Count是否为Dictionary.Value
//然后_totalDiscount+=字典[i].Discount.Price
退货-总折扣;
}
}
你知道如何解决这个问题吗,或者你对如何可能解决这个问题有想法吗
非常感谢 如果我正确理解了这个问题,也许这样做会奏效
foreach (var item in items)
{
if (item.Key.Discount.Count == item.Value)
_totalDiscount += item.Key.Discount.Price;
}
return __totalDiscount;
如果我正确理解这个问题,也许这样做会奏效
foreach (var item in items)
{
if (item.Key.Discount.Count == item.Value)
_totalDiscount += item.Key.Discount.Price;
}
return __totalDiscount;
您可以使用foreach遍历字典,如下所示:
public override decimal CalculateDiscount(IDictionary<myObject, int> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
foreach (var kvp in items)
{
if (kvp.Key.Discount.Count == kvp.Value)
_totalDiscount += kvp.Key.Discount.Price;
}
return _totalDiscount;
}
public覆盖十进制计算器(IDictionary项)
{
如果(items==null)抛出新的ArgumentNullException(nameof(items));
foreach(项目中的var kvp)
{
if(kvp.Key.Discount==kvp.Value)
_总折扣+=kvp.Key.Discount.Price;
}
退货-总折扣;
}
您可以使用foreach在字典中迭代,如下所示:
public override decimal CalculateDiscount(IDictionary<myObject, int> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
foreach (var kvp in items)
{
if (kvp.Key.Discount.Count == kvp.Value)
_totalDiscount += kvp.Key.Discount.Price;
}
return _totalDiscount;
}
public覆盖十进制计算器(IDictionary项)
{
如果(items==null)抛出新的ArgumentNullException(nameof(items));
foreach(项目中的var kvp)
{
if(kvp.Key.Discount==kvp.Value)
_总折扣+=kvp.Key.Discount.Price;
}
退货-总折扣;
}
使用Linq
//check if yourDictonary is not null
var sum = yourDictonary.Select(x => x.Key.Discount.Count == x.Value).Sum(x => x.Value)
使用Linq
//check if yourDictonary is not null
var sum = yourDictonary.Select(x => x.Key.Discount.Count == x.Value).Sum(x => x.Value)
_totalDiscountedValue=\u totalDiscountedValue+\u discountHandler.CalculatedScout(\u项)_totalDiscountedValue=\u totalDiscountedValue+\u discountHandler.CalculatedScout(\u项);我更喜欢这个答案,因为使用了保护条款。你介意解释一下“保护条款”是什么意思吗?这个答案和我的有什么不同?我更喜欢这个答案,因为使用了保护条款。你介意解释一下“保护条款”是什么意思吗?这个答案和我的有什么不同吗?
键。Dicount
应该是键。折扣
,OP有相同的打字错误,要检查if语句中的相等性,请使用=
而不是=
。语法!;)哦,我的错!在给出答案之前,我一定要测试运行我的代码,=
很糟糕!谢谢你的Key。Dicount
应该是Key。Discount
,OP有相同的打字错误,要检查if语句中的相等性,请使用=
而不是=
。语法!;)哦,我的错!在给出答案之前,我一定要测试运行我的代码,=
很糟糕!谢谢你很好的使用Linq!我不知道求和法,谢谢:-)很好地使用了Linq!我不知道求和法,谢谢:-)