0
這是類我有我怎樣才能獲得類的屬性值
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,個月2總...
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個屬性在列表中,你能簡單地將一個屬性添加到該類中,該屬性將這些屬性值作爲列表返回? – David
難道你不想讓'public int [] months = new int [5]'而不是這5個'int MonthX'嗎? –
@David我會這麼做 –