Linq两张表与sum列

时间:2018-01-08 15:33:27

标签: c# entity-framework linq

我的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    Year    Country      Admin.   UsedAdmin    Finance    UsedFinance
    1.   2017     USA           100         50         200         300
    2.   2017     China         300        300         500         400
    Total.                      400        350         700         700

请帮我解决我的模型设计和查询结果。谢谢。

1 个答案:

答案 0 :(得分:0)

因此,您希望将每个MultipleValue与UsedAmount连接在相同的年份值上。然后将结果分组为具有相同国家/地区的联接项目组。最后,从每个组创建一个具有国家/地区的对象,所有Admin值的总和以及所有财务值的总和。

// 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,
    }