开发者

Linq Group By list<Object> sum [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
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(); 


It is very unclear what you are trying to accomplish. Here are some problems with your code as it stands right now that will need to be addressed:

  1. Budget does not contain a definition of UsdActualRetail.
  2. List<BudgetAndActualLineInfoBySeasonDto> cannot be casted to List<Budget>.
  3. The group object cannot implicitly be casted to BudgetAndActualLineInfoBySeasonDto.
  4. I have already fixed misspellings of BUDGET, concat, and toList.
  5. In order to get the grouping to function, you will need to provide a custom implementation for equality comparison for your GroupBy object.

Making a bunch of wild educated guesses about how you want this code to work and what the definitions of the other missing classes might be, here is a (LinqPad-ready) complete working code sample that shows the grouped values and sums, with a working custom equality implementation:

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; }
}

Hope this helps! We will be able to help you better if you can improve your question considerably.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜