Linq Group By list<Object> sum [closed]
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:
Budget
does not contain a definition ofUsdActualRetail
.List<BudgetAndActualLineInfoBySeasonDto>
cannot be casted toList<Budget>
.- The
group
object cannot implicitly be casted toBudgetAndActualLineInfoBySeasonDto
. - I have already fixed misspellings of
BUDGET
,concat
, andtoList
. - 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.
精彩评论