您好我正在尝试通过此方法获取总金额
JavaFX
折扣百分比来自控件Swing
如何在下面的代码中实现它,谢谢
calculateDiscount = discount / 100
Discounted price = Unitprice * calculateDiscount
total amount = sum(Quantity * Discounted price)
答案 0 :(得分:0)
首先计算折扣,然后将其应用于Sum,如下面的代码:
double total = gridpur.Rows.Cast<GridViewRow>()
.Select(r => Tuple.Create((CheckBox)r.FindControl("chkSel"), (TextBox)r.FindControl("txtQuantity"), (TextBox)r.FindControl("txtUnitprice"), (TextBox)r.FindControl("txtDiscount")))
.Where(t => t.Item1.Checked)
.Sum(r => double.Parse(t.Item2.Text) * double.Parse(t.Item3.Text) * double.Parse(t.Item4.Text));
答案 1 :(得分:0)
我想你必须使用像
这样的东西calculateDiscount = discount / 100
double discounted = Unitprice * calculateDiscount
total amount = sum(Quantity * discounted)
这里的问题是Discounted price
不是变量的有效名称。
double total = gridpur.Rows.Cast<GridViewRow>()
.Where(r => ((CheckBox)r.FindControl("chkSel")).Checked)
.Sum(r => double.Parse(((TextBox)r.FindControl("txtQuantity")).Text) *
double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text));
答案 2 :(得分:0)
首先,这段代码真正开始需要ViewModel
或其他一些构造,使您能够将业务逻辑与UI分离。
话虽如此,快速而肮脏的答案可以帮助您实现临时linq查询,然后从中构建结果
// From your code
var validRows = gridpur.Rows.Cast<GridViewRow>()
.Where(r => ((CheckBox)r.FindControl("chkSel")).Checked);
var totals = validRows.Select((r =>
double.Parse(((TextBox)r.FindControl("txtQuantity")).Text)
* double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text));
如果我们从linq作为扩展方法转移到linq作为集成查询语法,它也会有所帮助,但随意混合和匹配
var validRows = from r in gridpur.Rows.Cast<GridViewRow>()
where ((CheckBox)r.FindControl("chkSel")).Checked)
select r;
var totals = from r in validRows
select double.Parse(((TextBox)r.FindControl("txtQuantity")).Text)
* double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text));
我们现在可以在查询中引入let语句和中间变量
var totals = from r in validRows
let quantity = double.Parse(((TextBox)r.FindControl("txtQuantity")).Text)
let unitPrice = double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text))
select quantity * unitPrice;
这样可以使查询更具可读性。
现在我们可以添加其他列和计算
var totals = from r in validRows
let quantity = double.Parse(((TextBox)r.FindControl("txtQuantity")).Text)
let unitPrice = double.Parse(((TextBox)r.FindControl("txtUnitprice")).Text))
let discountPercent = double.Parse(((TextBox)r.FindControl("txtDiscount")).Text) / 100.0d
select quantity * unitPrice * discountPercent;
var sumTotals = totals.Sum();