【问题标题】:Fill a list with objects depending on eachothers attributes根据彼此的属性用对象填充列表
【发布时间】:2014-03-05 08:44:51
【问题描述】:

我有一个列表,我想使用列表中创建的最新对象来计算下一个对象属性。我不确定如何编写循环来做到这一点。你能帮我解决这个问题吗?

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

【问题讨论】:

  • “我不确定如何编写循环来做到这一点。” ?将编辑文本,以便您更轻松地查看它

标签: c# list loops


【解决方案1】:

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

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】:

不要认为你会使用循环,

您所要做的就是让构造函数将 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]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 2020-02-04
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多