I have a problem. I am getting the sum of a certain column from the database. However, in some cases the sum should be 0 -> if there are no records that meet the criteria
In the view I have:
<Label Content="{Binding Path=Sum, UpdateSourceTrigger=PropertyChanged}" />
In ViewModel I have:
public override void save()
{
Sum = new Sum(ATMAEntites).Calculate(IdEvent, IdDealer, "Standing");
}
In the model class I have:
public int? Calculate(int IdEvent, int IdDealer, string CategoryName)
{
return (from wydaneSprzedawcy in ATMAEntites.WydaneSprzedawcy
where wydaneSprzedawcy.Bilety.IdEvent == IdEvent &&
wydaneSprzedawcy.IdDealer == IdDealer &&
wydaneSprzedawcy.Bilety.CategoryName == CategoryName
select wydaneSprzedawcy.quantity).Sum();
}
I want check in the model whether the query returns null
I assume, that wydaneSprzedawcy.quantity is int and not nullable. Then you can try adding a cast to (int?) in the select, like this:
public int? Calculate(int IdEvent, int IdDealer, string CategoryName)
{
return
(
from wydaneSprzedawcy in ATMAEntites.WydaneSprzedawcy
where
wydaneSprzedawcy.Bilety.IdEvent == IdEvent &&
wydaneSprzedawcy.IdDealer == IdDealer &&
wydaneSprzedawcy.Bilety.CategoryName == CategoryName
select (int?)wydaneSprzedawcy.quantity
).Sum();
}
Now, if the query returns no records, sum should return null.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.