I was getting an error when I was execting the following query that summed a non-nullable decimal. The magnitude was null when there was no magnitude value for a location/organization combination.
let q = query { from m in Measure do
where (locations.Contains(m.Location)
&& organizations.Contains(m.Organization))
sumBy m.Magnitude }
Error: The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.
I solved this in the following way. Is this there a better way to do this?
let convert (n:Nullable<decimal>) =
let c:decimal = 0M
if n.HasValue then n.Value else c
let q = query { from m in Measure do
let nullable = Nullable<decimal>(m.Magnitude)
where (locations.Contains(m.Location)
&& organizations.Contains(m.Organization))
sumBy (convert(nullable)) }
Thanks. I changed my query to the following.
query { for m in db.Measurement do
let nullable = Nullable<decimal>(m.Magnitude)
where ( m.Measure = me
&& (if slice.Organization > 0L then organizationUnits.Contains( m.PrimalOrganizationUnit.Value ) else true)
&& (if slice.Location > 0L then locationUnits.Contains(m.PrimalLocationUnit.Value) else true) )
sumByNullable (nullable) }
sumByNullable?