根据每个属性填充包含对象的列表

时间:2014-03-05 08:44:51

标签: c# list loops

我有一个List,我想使用List中创建的最新对象来计算下一个对象属性。我不知道如何编写循环这样做。你能帮帮我吗?

public ActionResult ShowDetail(DateTime startdate, double PresentValue, double InterestRate, double FutureValue, int PaymentPeriods)
    {
        List<Calculation> cList = new List<Calculation>();
        Calculation calc = new Calculation();
        calc.Date = startdate.ToShortDateString();
        calc.InvoiceAmount = 2000;
        calc.InterestRate = InterestRate;
        calc.InterestAmount = (PresentValue * InterestRate / 360 * 30);
        calc.Amortization = (2000 - (PresentValue * InterestRate / 360 * 30));
        calc.PresentValue = PresentValue;
        cList.Add(calc);
        for (int i = 0; i < PaymentPeriods; i++)
        {
            cList.Add(new Calculation()
                {
                    var calcBefore = cList.GetLastObject //Some how I want to take the object before the one i want to create
                    cList.Add(new Calculation()
                    {
                        Date = calcBefore.Date.Add(1).Month() //something like this
                        InvoiceAmount = calcBefore.InvoiceAmount
                        InterestRate = calcBefore.InterestRate
                        InterestAmount = (calcBefore.PresentValue * InterestRate / 360 * 30) //I want to take the presentvalue from the object before in the List and use that to calculate the the next InterestAmount
                        //And so on
                    }
                });
        }

        return PartialView("ShowDetail", cList);
    }

计算:

public partial class Calculation
{
   public string Date { get; set; }
   public double InvoiceAmount { get; set; }
   public double InterestRate { get; set; }
   public double InterestAmount { get; set; }
   public double Amortization { get; set; }
   public double PresentValue { get; set; }
}

2 个答案:

答案 0 :(得分:2)

您可以使用列表索引访问最后插入的内容:

var calcBefore = cList[cList.Count - 1];

另一种做同样的方式:Enumerable.Last

var calcBefore = cList.Last();

由于您在循环之前添加了一个,因此列表不为空,这是安全的。

这是完整的循环:

for (int i = 0; i < PaymentPeriods; i++)
{
    calc = new Calculation();
    Calculation calcBefore = cList[cList.Count - 1];
    calc.Date = DateTime.Parse(calcBefore.Date).AddMonths(1).ToString();
    calc.InvoiceAmount = calcBefore.InvoiceAmount;
    calc.InterestRate = calcBefore.InterestRate;
    calc.InterestAmount = (calcBefore.PresentValue * InterestRate / 360 * 30);//I want to take the presentvalue from the object before in the List and use that to calculate the the next InterestAmount
    cList.Add(calc);
}

根据Date,我假设您要添加一个月,请使用DateTime.AddMonths

Date = DateTime.Parse(calcBefore.Date).AddMonths(1).ToString();

但是,我根本不会String使用DateTime

答案 1 :(得分:0)

不认为你会使用循环,

您所要做的就是让构造函数将Calculation对象作为参数。

然后你会做类似

的事情
public partial class Calculation
{
   public string Date { get; set; }
   public double InvoiceAmount { get; set; }
   public double InterestRate { get; set; }
   public double InterestAmount { get; set; }
   public double Amortization { get; set; }
   public double PresentValue { get; set; }

public   Calculation (Calculation as calculation(
   {
    ..  set you prop
   }
}

然后当你调用一个新对象时,你传入当前列表中的最后一个对象

var newObj = new Calculation(cList [cList.Length -1]);