Linq C#Group by Sum [英] Linq C# Group by Sum
本文介绍了Linq C#Group by Sum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际数据
ID |数量| UnitPrice
1 2 10.00
1 3 30.00
我想组通过这个具有相同ID的表,但是它显示了这样的两行输出
失败输出
ID |总计
1 20.00
1 90.00
但实际上我希望我的输出是这样的
ID |总计
1 110.00
请告知我因为linq还是新手,非常感谢
Actual Data
ID | Qty | UnitPrice
1 2 10.00
1 3 30.00
I wanna group by this table with same ID, but it show put two row of output like this
Fail Output
ID | Total
1 20.00
1 90.00
but actually i wish my output is like this
ID | Total
1 110.00
pls kindly advise cause i still new in linq , thank alot
public DataTable GetQuotationInfo(int QuotationId)
{
FADB db = new FADB();
DataTable dt = new DataTable();
dt.Columns.Add("QuotationId");
dt.Columns.Add("ServiceOrderNo");
dt.Columns.Add("Date");
dt.Columns.Add("Name");
dt.Columns.Add("OnwerAddress");
dt.Columns.Add("QuotationNo");
dt.Columns.Add("Premises");
dt.Columns.Add("PayableTo");
dt.Columns.Add("Total");
dt.Columns.Add("Qty");
dt.Columns.Add("UnitNo");
dt.Columns.Add("Status");
dt = (from q in db.Csm_quotations
join j in db.Csm_quotation_jobs on q.QuotationId equals j.QuotationId
join c in db.Csm_complaints on q.ComplaintId equals c.ComplaintID
join f in db.Fa_lots on c.UnitId equals f.UnitId
join o in db.Sys_owners on f.OwnerId equals o.OwnerId
group new { q,j,c,f,o } by new { q.QuotationId,c.ComplaintCode,q.QuotationDate,q.QuotationSubject,q.PayableTo,q.Status,f.OwnerAddress,f.Address,o.OwnerName,j.Qty,j.UnitPrice }
into grp
where grp.Key.QuotationId == Convert.ToUInt16(QuotationId)
select new
{
QuotationId = grp.Key.QuotationId,
ServiceOrderNo = grp.Key.ComplaintCode,
Date = grp.Key.QuotationDate.ToString("dd/MM/yyyy"),
Name = grp.Key.OwnerName,
OnwerAddress =grp.Key.OwnerAddress,
QuotationNo = grp.Key.QuotationSubject,
Premises = grp.Key.Address,
PayableTo = grp.Key.PayableTo,
Total = Convert.ToDouble(grp.Sum(x => (grp.Key.Qty * grp.Key.UnitPrice))) // as i know this line having problem, pls giving any suggestion
}).ToDataTable();
return dt;
}
推荐答案
您好b $ b
请添加此行
Hi
Please add this line
Status = grp.Key.Status,
以上总计
其他明智的你可以从分组中移除状态
并检查
above Total line
other wise you can remove Status from grouping
and check
使用此查询
use this query
from mytable in db.mytable
group mytable by new {
mytable.id
} into g
select new {
id = (System.Int32?)g.Key.id,
TotalAmt = (System.Int32?)g.Sum(p => p.Qty * p.Unitprice)
}
这篇关于Linq C#Group by Sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文