PDA

View Full Version : سوال: کنترل خطای Nullable object must have a value



nezhad313
یک شنبه 18 فروردین 1398, 23:47 عصر
باسلام خدمت اساتید می خواستم در خصوص اینکه بنده متد زیر را نوشتم و یک خروجی دسیمال را برای من بر می گرداند ولی ارور(Nullable object must have a value.) را نشان می دهد میخواستم راهنمایی بفرمائید چطوری بتوانم ضمن کنترل خطا در صورت خالی بودن مقدار صفر را برگرداند .با تشکر
private decimal GetKharid(int year, int month) {
return db.FactorDetaile.Where(a => a.Factor.DateOfBuy.Year == year && a.Factor.DateOfBuy.Month == month && a.Factor.IsBuy == true).Sum(a => a.TotalPrice).Value;
}

Mahmoud.Afrad
دوشنبه 19 فروردین 1398, 00:34 صبح
private decimal GetKharid(int year, int month)
{
var s = db.FactorDetaile.Where(a => a.Factor.DateOfBuy.Year == year && a.Factor.DateOfBuy.Month == month && a.Factor.IsBuy == true).Sum(a => a.TotalPrice);
return (s==null ? 0 : s);
}

samie_shamsi
چهارشنبه 05 آبان 1400, 14:49 عصر
private decimal GetKharid(int year, int month)
{
var s = db.FactorDetaile.Where(a => a.Factor.DateOfBuy.Year == year && a.Factor.DateOfBuy.Month == month && a.Factor.IsBuy == true).Sum(a => a.TotalPrice);
return (s==null ? 0 : s);
}

نفست طلا داداش ، البته از این Syntax هم استفاده بشه عالیه:

public int getSum(int id , string firstDate , string lastDate){
return db.Order_Partner.Where(c=> c.id == id && Convert.ToDateTime(firstDate) <= c.Date && c.Date <= Convert.ToDateTime(lastDate).Select(l=> l.Price).DefaultIfEmpty(0).Sum(); )
}