【发布时间】:2023-04-11 05:06:03
【问题描述】:
这是我的课
public class Income
{
public string Code { get; set; }
public int Month1 { get; set; }
public int Month2 { get; set; }
public int Month3 { get; set; }
public int Month4 { get; set; }
public int Month5 { get; set; }
public List<Income> income { get; set; }
}
在另一个班级
List<Income> incomeList = new List<Income>();
//repeat twice
Income obj = new Income();
obj.Month1 = 200;
obj.Month2 = 150;
...
IncomeList.Add(obj);
obj.income = IncomeList;
现在我想为每个保存在 列表。 目前为止
Int[] results = obj.income
.Select(x=> new
{
x.Month1,
x.Month2,
x.Month3,
x.Month4,
x.Month5
})
.ToArray();
这是我需要为每个唯一对象添加总月数的地方。 获取所有 Months1 、 Months2 的总数 ...
double totals[] = new double[5];
for (int i=0;i<results.length;i++)
{
totals[i] += results[i]; // I get the first object reference
// I want Moth1,Month2 ... to be in an indexed array where
// if i want Month1 i would access similar to : results[index];
}
【问题讨论】:
-
不确定我是否 100% 清楚您要做什么。但是,如果您希望这 5 个属性在列表中,您是否可以简单地向该类添加一个属性,将这些属性值作为列表返回?
-
你不想用
public int[] months = new int[5]而不是这5个int MonthX吗? -
@David 我会这样做的
-
@Gariel J 另一个类用独特的代码表示来自数据库的数据,为什么,感谢您的建议
标签: c#