【发布时间】:2014-01-10 16:58:26
【问题描述】:
我正在尝试应用一些业务逻辑。
我关于在以下对象模型上使用 LINQ 以应用业务逻辑的问题。我相应地填充了以下对象:
public class Waiver
{
public string Id { get; set; }
public int Type { get; set; }
public decimal Amount { get; set; }
}
要应用的业务逻辑:
1.) 应用订单项豁免
如果 LineItem Waiver [Type] 为 1111,则从单价中扣除 LineItem Waiver 金额
如果 LineItem Waiver [Type] 为 2222,则减去 LineItem WaiverAmount 作为单价的百分比
如果 LineItem Waiver [Type] 为 3333,则减去 LineItem Waiver 金额(行价格 = 数量 * 单价)
如果 LineItem Waiver [Type] 为 4444,则将 LineItem Waiver 金额作为离线价格的百分比扣除
2.) 申请订单豁免
如果订单豁免 [类型] 为 4444,则在应用订单项豁免后从总订单价格中扣除订单豁免金额
如果订单豁免 [类型] 为 8888,则在应用 LineItem 豁免后,将订单豁免金额作为订单价格的百分比扣除
实现这一目标的最佳方法是什么?
GetWaivedPrice(decimal unitPrice, int qty, IEnumerable<Waiver> waiver)
GetWaivedPrice 可以写成单个 LINQ 方法,并为所有折扣类型提供适当的映射吗?
这就是我想要实现的目标,最好是编写良好的 LINQ 方法:
private decimal GetWaivedPrice(decimal unitPrice, int qty,
IEnumerable<Waiver> waiver)
{
//Pseudo code shown for clarifying intent
decimal waivedLineItemAmount = 0m;
if waiver.Select(d => d.Type == 1111)
//then apply the business logic on the unit price accordingly
if waiver.Select(d => d.Type == 2222)
//then apply the business logic on the unit price accordingly
if waiver.Select(d => d.Type == 3333)
//then apply the business logic on the unit price accordingly
return waivedLineItemAmount;
}
【问题讨论】:
-
你能更具体一点你有什么问题吗?为什么 JSON 是关于对数据进行一些(看似微不足道的)操作的问题的一部分?对于这种情况,您对“更好”的定义是什么(可读性、代码大小、行数……)?
-
我的问题是编写 GetWaivedPrice 方法以应用行级豁免的最佳方式。 JSON 只是作为输入外观的示例格式。
-
我已删除所有不相关的 JSON 代码、构造函数和空格 - 如有必要,请随时改进。请展示您对实现
GetWaivedPrice的尝试 - 当前方法签名表明您不太可能尝试编写一个...此外,您仍然需要添加“更好”的标准... -
投票重新开放... 旁注:我知道大多数人一次不能阅读超过一份声明(我就是其中之一:)。因此,让我们尝试单一陈述:请提供您选择“最佳方式”的标准——在您的情况下,什么是“更好”?