C# Linq在一个表中获取父级和子级,并在另一个表中计算它们的值
我有两张桌子: ParentID | ChildID -------- ------- 91209 91210 91209 91211 91212 91213 91212 91214 ParentID | ChildID -------- ------- 91209 91210 91209 91211 91212 91213 91212 91214 记录ID |数量 -------- --------- 91209 1500 91210 2500 91211 7200 91212 6000 91213 5000 91214 6000 我希望结果是: RecordId | Total Quantity -------- -------------- 91209 11200 91212 17000 记录ID |总数量 -------- -------------- 91209 11200 91212 17000C# Linq在一个表中获取父级和子级,并在另一个表中计算它们的值,c#,mysql,asp.net-mvc,linq,C#,Mysql,Asp.net Mvc,Linq,我有两张桌子: ParentID | ChildID -------- ------- 91209 91210 91209 91211 91212 91213 91212 91214 ParentID | ChildID -------- ------- 91209 91210 91209 91211 91212 91213 91212 91214 记录ID |数量 --------
我尝试了一些复杂的连接查询,但似乎没有得到所需的结果。如果您有任何建议,我们将不胜感激。假设表名为acc
var parentRecords = records.Select(x =>
new Record { RecordID = parents.FirstOrDefault(y => y.ChildID == x.RecordID || y.ParentID == x.RecordID).ParentID, Quantity = x.Quantity});
var result = parentRecords.GroupBy(z => z.RecordID,
(key, value) => new { RecordID= key, TotalQuantity= value.Sum(a => a.Quantity) });
假设表名为acc
var parentRecords = records.Select(x =>
new Record { RecordID = parents.FirstOrDefault(y => y.ChildID == x.RecordID || y.ParentID == x.RecordID).ParentID, Quantity = x.Quantity});
var result = parentRecords.GroupBy(z => z.RecordID,
(key, value) => new { RecordID= key, TotalQuantity= value.Sum(a => a.Quantity) });
明亮的编辑工作很有魅力。非常感谢你的帮助!很高兴它有帮助!请考虑接受我的回答。编辑工作很有魅力。非常感谢你的帮助!很高兴它有帮助!请考虑接受我的回答。