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();
答案 0 :(得分:2)
目前还不清楚你想要完成什么。以下是您现在需要解决的代码问题:
Budget
不包含UsdActualRetail
的定义。List<BudgetAndActualLineInfoBySeasonDto>
无法投放到List<Budget>
。group
对象投放到BudgetAndActualLineInfoBySeasonDto
。BUDGET
,concat
和toList
的拼写错误。通过一系列疯狂的猜测,了解您希望此代码如何工作以及其他缺失类的定义可能是什么,这里是一个(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; }
}
希望这有帮助!如果您可以大大改善您的问题,我们将能够更好地帮助您。