代码之家  ›  专栏  ›  技术社区  ›  MrProgram

根据每个属性用对象填充列表

  •  0
  • MrProgram  · 技术社区  · 12 年前

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

    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 回复  |  直到 12 年前
        1
  •  2
  •   Tim Schmelter    12 年前

    您可以使用列表的索引访问上次插入的:

    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 完全

        2
  •  0
  •   Anthony    12 年前

    不要以为你会使用循环,

    您需要做的就是让构造函数将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=新计算(cList[cList.Length-1]);

    推荐文章