【问题标题】:Linq two table with sum column带有总和列的Linq两个表
【发布时间】:2018-01-08 15:33:26
【问题描述】:

我的 RowMultiplevaluw 表是

public class RowMultipleValues
{
    public int ID { get; set; }       
    public String Year{ get; set; }
    public string country      { get; set; }
    public decial Admin { get; set; }
    public  decimal Finance { get; set; }
    public virtual ICollection<UsedAmount> UsedAmount { get; set; }        
}

我的使用量表是

public class UsedAmount
{
    public int ID { get; set; }       
    public string  Year{ get; set; }
    public string country      { get; set; }     
    public decial  UsedAmount { get; set; }

    public int RowMultipleValues ID { get; set; } 
    Public virtual RowMultibleValue RowMultibleValue { get; set; }

 }

我的查询是

var query = from mtv in context.multiplerowvaluetable
            join usd in dbcontext.usedtsble on mtv.year equal usd.year group g by mtv.country into g
            select new { country =g.key,sumadmincolumn =g.sum(Admin),sumfinancecolumn = g.sum(finance) }).tolist();

我想要的结果是

ID 年份国家/地区管理员。 UsedAdmin 财务 UsedFinance 1. 2017 美国 100 50 200 300 2. 2017 中国 300 300 500 400 全部的。 400 350 700 700

请帮我设计模型并查询结果。谢谢。

【问题讨论】:

  • 如何先清理你的代码,并包含你期望的输入和输出
  • 这行不通:contextdbcontext 在一个 LINQ 语句中。

标签: c# entity-framework linq


【解决方案1】:

因此,您希望将每个 MultipleValue 与 UsedAmount 的年份值相等。然后将结果分组为具有相同国家/地区的连接项目组。最后从每个组中创建一个包含国家/地区的对象,即所有 Admin 值的总和和所有 Finance 值的总和。

// first join the two collections on same year.
// we only need properties Country, Admin, Finance:
var result = myDbContext.MultipleRowValueTable.Join(myDbContext.UsedAmountTable,
    multipleRow => multipleRow.Year,    // from every multipleRow take the year
    usedAmount => usedAmount.Year,      // from every usedAmount take the year
    (multipleRow, usedAmount) => new    // when they match make a new object
    {
        Country = multipleRow.Country,
        Admin = multipleRow.Admin,
        UsedAdmin = usedAmount.Admin,
        Finance = multipleRow.Finance,
        UsedFinance = usedAmount.Finance,
    })
    // group the elements from this join table into groups with same Country
    .GroupBy(joinedItem => joinedItem.Country,  // all items in the group have this Country
         joinedItem => new                      // the elements of the group
         {
             Admin = joinedItem.Admin,
             UsedAdmin = joinedItem.UsedAdmin,
             Finance = joinedItem.Finance,
             UsedFinance = joinedItem.UsedFinance,
         })

         // finally: from every group take the Key (which is the Country) 
         // and the sum of the Admins and Finances in the group
         .Select(group => new
         {
              Country = group.Key,
              SumAdminColumn = group
                  .Select(groupElement => groupElement.Admin)
                   .Sum(),
              ... // others are similar
         });


    // from every group take the elements and sum the properties
    .Select(group => new
    {
        Id = multipleRowValue.Id,
        Year = multipleRowValue.Year,
        Country = multipleRowValue.Country,
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多