0

I have seen many solution about this error but still I am unable to apply it in my query. Kindly help. Below is my code:

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

The problem is when pp.PlanDuration1 = 0 then there is no any matching row to check in the table db.PlanDetail:

where pp.PlanDuration1 == pd.PlanDetailID

I was trying to use .Any() and also

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

But still I am unable to figure it out. Kindly help.

Karishma
  • 199
  • 4
  • 12
  • Try using null coalesce operator inside query: `PlanDuration1 = planduration1.durationInMonths ?? 0` or use `DefaultIfEmpty(0)`. See http://stackoverflow.com/questions/15297925/the-cast-to-value-type-double-failed-because-the-materialized-value-is-null for similar problem. – Tetsuya Yamamoto Apr 13 '17 at 07:58
  • Thanks for replying. But I have tried both the things and none of them works! First option gives the error "Operator ?? cannot be applied to operands of type double and int". Second option gives the error "IQueryable does not contain a definition for DefaultEmpty" – Karishma Apr 13 '17 at 08:01
  • Use Convert.ToDouble() – Zahid Mustafa Apr 13 '17 at 09:45

0 Answers0