C# 根据每个对象的属性,用对象填充列表

C# 根据每个对象的属性,用对象填充列表,c#,list,loops,C#,List,Loops,我有一个列表,我想使用列表中创建的最新对象来计算下一个对象属性。我不知道如何编写这样的循环。你能帮我吗 public ActionResult ShowDetail(DateTime startdate, double PresentValue, double InterestRate, double FutureValue, int PaymentPeriods) { List<Calculation> cList = new List<Calcula

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

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);
    }

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

var calcBefore = cList[cList.Count - 1];
另一种实现相同功能的方法:
可枚举。最后一个

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);
}

但是,我根本不会使用
字符串
作为
日期时间

不要认为您会使用循环

您需要做的就是让构造函数将计算对象作为参数

然后你会做一些类似的事情

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])

“我不知道如何编写这样做的循环。”?将编辑文本以便您可以更轻松地使用该字符串。该字符串仅用于测试,但现在将对其进行更改:)也将测试您的代码
Date = DateTime.Parse(calcBefore.Date).AddMonths(1).ToString();
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
   }
}