Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 转换为值类型';系统。双';由于物化值为null,因此失败_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 转换为值类型';系统。双';由于物化值为null,因此失败

Asp.net mvc 转换为值类型';系统。双';由于物化值为null,因此失败,asp.net-mvc,Asp.net Mvc,我已经看到了很多关于这个错误的解决方案,但是我仍然无法在我的查询中应用它。请帮忙。下面是我的代码: public ActionResult Index() { var viewModels = (from pp in db.PlanPackages //let checkplandurationidifnull = (double?)pp.PlanDuration1 ?? 0

我已经看到了很多关于这个错误的解决方案,但是我仍然无法在我的查询中应用它。请帮忙。下面是我的代码:

public ActionResult Index()
    {
        var viewModels = (from pp in db.PlanPackages
                          //let checkplandurationidifnull = (double?)pp.PlanDuration1 ?? 0
                          let planduration1 = (from pd in db.PlanDetail
                                               where pp.PlanDuration1 == pd.PlanDetailID
                                               select pd).FirstOrDefault()
                          let planduration2 = (from pd in db.PlanDetail
                                               where pp.PlanDuration2 == pd.PlanDetailID
                                               select pd).FirstOrDefault()
                          let planduration3 = (from pd in db.PlanDetail
                                               where pp.PlanDuration3 == pd.PlanDetailID
                                               select pd).FirstOrDefault()
                          let planduration4 = (from pd in db.PlanDetail
                                               where pp.PlanDuration4 == pd.PlanDetailID
                                               select pd).FirstOrDefault()
                          select new _PlanDetailPackageDisplay()
                          {
                              PlanID1 = pp.PlanID1,
                              PlanID2 = pp.PlanID2,
                              PlanID3 = pp.PlanID3,
                              PlanID4 = pp.PlanID4,
                              PlanDuration1 = planduration1.durationInMonths,
                              PlanDuration2 = planduration2.durationInMonths,
                              PlanDuration3 = planduration3.durationInMonths,
                              PlanDuration4 = planduration4.durationInMonths,
                              PlanPrice1 = planduration1.price,
                              PlanPrice2 = planduration2.price,
                              PlanPrice3 = planduration3.price,
                              PlanPrice4 = planduration4.price,
                              PackageDiscout = pp.discount,
                              PackageStatus = pp.StatusPlanID
                          }).ToList();


        return View(viewModels);
    }
问题在于,当pp.PlanDuration1=0时,表db.PlanDetail中没有任何匹配行可供检查:

where pp.PlanDuration1 == pd.PlanDetailID
我试图使用.Any()和

let checkplandurationidifnull = (double?)pp.PlanDuration1 ?? 0

但我还是无法理解。请提供帮助。

尝试在查询中使用空合并运算符:
PlanDuration1=PlanDuration1.DurationNMonths??0
或使用
DefaultIfEmpty(0)
。有关类似问题,请参阅。谢谢回复。但我已经尝试了这两种方法,但没有一种有效!第一个选项给出错误“运算符??不能应用于double和int类型的操作数”。第二个选项使用Convert.ToDouble()给出错误“IQueryable不包含DefaultEmpty的定义”