Linq GroupBy和来自存储过程的MVVM的Sum

时间:2016-09-07 14:34:47

标签: c# asp.net-mvc linq

我查看了其他帖子,但无法找到我的上下文所需的确切内容。 当我不尝试使用linq添加组合时,我的程序工作正常。我使用MVVM从存储过程中提取数据。我的模特是

public class RelationshipExperience
{
    [Key, Column(Order = 1)]
    public double Earned { get; set; }

    [Key, Column(Order = 2)]
    public double IncLoss { get; set; }

    [Key, Column(Order = 3)]
    public double Expenses { get; set; }

    [Key, Column(Order = 4)]
    public double Balance { get; set; }

    [Key, Column(Order = 5)]
    public string UWYear { get; set; }

    public int ProgramID { get; set; }

    public int InProgramChain { get; set; }
}

我当前的工作方法是

public IList<RelationshipExperience> sp_GetRelationshipExperience(int programId)
{
    return _CatContext.sp_GetRelationshipExperience(programId).ToList();
}

我想总结我正在使用的所有列(获得,IncLoss,费用,余额)和UWYear组合。这是我的尝试

    public IList<RelationshipExperience> sp_GetRelationshipExperience(int programId)
    {
        return _CatContext.sp_GetRelationshipExperience(programId)
        .GroupBy(l => l.UWYear)
        .Select(r => new RelationshipExperience
        {
            UWYear = r.Key,
            Earned = r.Sum(c => c.Earned),
            IncLoss = r.Sum(c => c.IncLoss),
            Expenses = r.Sum(c => c.Expenses),
            Balance = r.Sum(c => c.Balance)

        }).ToList();
    }
我做错了什么? 提前致谢

1 个答案:

答案 0 :(得分:0)

    public IList<RelationshipExperience> sp_GetRelationshipExperience(int programId, bool AllPrograms)
    {
            var output = _CatContext.sp_GetRelationshipExperience(programId)
.Where(l => l.InProgramChain == 1)
.GroupBy(l => l.UWYear)
.Select(r => new RelationshipExperience
{
    UWYear = r.Key,
    Earned = r.Sum(c => c.Earned),
    IncLoss = r.Sum(c => c.IncLoss),
    Expenses = r.Sum(c => c.Expenses),
    Balance = r.Sum(c => c.Balance),

}).ToList();

            return output;

        }

这有效,在我的数据网格中显示结果