Linq Group按列表<object> sum </object>

时间:2011-07-15 22:20:08

标签: linq list

public class Budget
{
    public string SeasonNo { get; set; }
    public string SeasonName { get; set; }
    public string typeProductCd { get; set; }
    public string typeProductDescription { get; set; }
    public Decimal UsdBudgetRetail { get; set; }
}

List<Budget> list1

List<Budget> list2

List<Budget> ListUngrouped =
    list1.Concat(list2).ToList();

List<Budget> listGrouped = ListUngrouped
    .GroupBy(x => new { x.SeasonNo, x.typeProductCd })
    .Select(group => new { group.Key, UsdBudgetRetail = group.Sum(x => x.UsdActualRetail) })
    .Cast<BudgetAndActualLineInfoBySeasonDto>()
    .ToList(); 

1 个答案:

答案 0 :(得分:2)

目前还不清楚你想要完成什么。以下是您现在需要解决的代码问题:

  1. Budget不包含UsdActualRetail的定义。
  2. List<BudgetAndActualLineInfoBySeasonDto>无法投放到List<Budget>
  3. 无法隐式将group对象投放到BudgetAndActualLineInfoBySeasonDto
  4. 我已修复BUDGETconcattoList的拼写错误。
  5. 为了使分组正常运行,您需要为GroupBy对象提供自定义实现以进行相等比较。
  6. 通过一系列疯狂的猜测,了解您希望此代码如何工作以及其他缺失类的定义可能是什么,这里是一个(LinqPad-ready)complete working code sample,它显示了分组的值和总和,使用有效的自定义等式实现:

    void Main()
    {
        List<Budget> list1 = new List<Budget>() {
            new Budget { SeasonNo = "1", SeasonName = "Test 1", typeProductCd = "3", UsdActualRetail = 1.01m },
            new Budget { SeasonNo = "1", SeasonName = "Test 2", typeProductCd = "3", UsdActualRetail = 1.01m },
            new Budget { SeasonNo = "1", SeasonName = "Test 3", typeProductCd = "3", UsdActualRetail = 1.01m },
            new Budget { SeasonNo = "1", SeasonName = "Test 4", typeProductCd = "3", UsdActualRetail = 1.01m },
        };
    
        List<Budget> list2 = new List<Budget>() {
            new Budget { SeasonNo = "2", SeasonName = "Test 5", typeProductCd = "4", UsdActualRetail = 1.02m },
            new Budget { SeasonNo = "2", SeasonName = "Test 6", typeProductCd = "4", UsdActualRetail = 1.02m },
            new Budget { SeasonNo = "2", SeasonName = "Test 7", typeProductCd = "4", UsdActualRetail = 1.02m },
        };
    
        List<Budget> listUngrouped = list1.Concat(list2).ToList();
    
        List<BudgetAndActualLineInfoBySeasonDto> listGrouped = 
            listUngrouped
                .GroupBy(x => new SeasonDto { SeasonNo = x.SeasonNo, typeProductCd = x.typeProductCd })
                .Select(group => new BudgetAndActualLineInfoBySeasonDto {
                    SeasonDto = group.Key,
                    UsdBudgetRetail = group.Sum(x => x.UsdActualRetail)
                })
                .Cast<BudgetAndActualLineInfoBySeasonDto>()
                .ToList();
        Console.WriteLine(listGrouped);
    }
    
    // Define other methods and classes here
    
    public class SeasonDto
    {
        public string SeasonNo { get; set; }
        public string typeProductCd { get; set; }
    
        public override bool Equals(object other)
        {
            SeasonDto otherS = other as SeasonDto;
            if (otherS != null)
            {
                return this.SeasonNo.Equals(otherS.SeasonNo) &&
                    this.typeProductCd.Equals(otherS.typeProductCd);
            }
            else
            {
                return false;
            }
        }
    
        public override int GetHashCode()
        {
            return SeasonNo.GetHashCode() + typeProductCd.GetHashCode();
        }
    }
    
    public class BudgetAndActualLineInfoBySeasonDto
    {
        public SeasonDto SeasonDto { get; set; }
        public decimal UsdBudgetRetail { get; set; }
    }
    
    public class Budget
    {
            public string SeasonNo { get; set; }
            public string SeasonName { get; set; }
            public string typeProductCd { get; set; }
            public string typeProductDescription { get; set; }
            public Decimal UsdActualRetail { get; set; }
    }
    

    希望这有帮助!如果您可以大大改善您的问题,我们将能够更好地帮助您。