我如何找出谁是最年轻的司机列表

时间:2017-03-06 18:33:38

标签: c# winforms entity-framework

我需要能够计算最年轻的司机,然后将政策变更金额应用于其值,即是否存在于某个年龄段之间。

public void UpdatePolicyPremium(int policyid,Decimal amount)
    {

        List<tblDriver> driver = appliedDB.tblDrivers.Where(b => b.policy_Id == policyid).ToList();
        foreach (var drivers in driver)
        {
         if (drivers.age >21 && drivers.age <25) i should increase by 20% if youngest driver
            {

                tblPolicy _policy = appliedDB.tblPolicies.Where(b => b.id ==  policyid).SingleOrDefault();

                _policy.amount = _policy.amount + amount;
            }

            if (drivers.age > 26 && drivers.age < 75)// i should increase by 10 percent if youngest driver
            {

                tblPolicy _policy = appliedDB.tblPolicies.Where(b => b.id == policyid).SingleOrDefault();

                _policy.amount = _policy.amount + amount;
            }
        }

        appliedDB.SaveChanges();

    }

但是程序必须弄清楚谁是这个范围内最长的司机,我不知道我会怎么做呢

驱动程序表 ID 名字 姓 DOB 年龄

政策表 ID 司机ID 开始日期 量 isIncreaseOnPolicy

重新发布    我已经尝试了Gert Arnold的选项,它看起来确实有效,但我还需要增加政策开始日期的功能

这是任务

•如果最年轻的司机在保单开始日期年龄介于21至25岁之间,保费增加20%

即BaseAmount = 500因此,如果在开始日期年龄介于21至25岁之间,应增加10%的增加10%

1 个答案:

答案 0 :(得分:2)

按年龄订购司机并获得第一个司机。这个驱动程序将是最年轻的。代码如下(尚未填写)

public void UpdatePolicyPremium(int policyid, Decimal amount)
{
   tblDriver driver = appliedDB.tblDrivers.Where(b => b.policy_Id == policyid).OrderBy(r =>r.Age).FirstOrDefault();
   if (driver != null)
   {
      if (driver.age > 21 && driver.age < 25) //i should increase by 20 % if youngest driver
      {
         tblPolicy _policy = appliedDB.tblPolicies.Where(b => b.id == policyid).SingleOrDefault();
         _policy.amount = _policy.amount * 1.2;
      }

      if (driver.age > 26 && driver.age < 75)// i should increase by 10 percent if youngest driver
      {
         tblPolicy _policy = appliedDB.tblPolicies.Where(b => b.id == policyid).SingleOrDefault();
         _policy.amount = _policy.amount * 1.1;
      }
   }
   appliedDB.SaveChanges();
}