I have this code a developer wrote. I think its horrible and shouldnt be necessary
value = s.Businesses.SelectMany(
x => x.Payments.Where(
w => w.total != 0 &&
!w.jobId.HasValue &&
w.createdAt >= Utility.monthS
&& w.createdAt <= Utility.monthE)
).Any() ?
s.Businesses.SelectMany(
x => x.Payments.Where(
w => w.total != 0 &&
!w.jobId.HasValue &&
w.createdAt >= Utility.monthS
&& w.createdAt <= Utility.monthE)
).Sum(su => su.quantity)
: 0;
The reason it does the .Any
before the Sum
is that records with no values end up getting null values and causing errors.
Is there a better best practice way of writing this.