0

I need some syntactic help with a Linq statement. The overall statement is lengthy and selects against a SQL Server database over which I have no design control.

In my select new phrase I have the following column;

              on_Hand_Qty1 = (from x1 in Material_Locations
                             where x1.Material == j.Part_Number
                                 && x1.Location_ID != "MSSICONSMT"
                             select x1.On_Hand_Qty ?? 0.0).Sum()
                             ,

In LINQPad I get an error of: Operator '??' cannot be applied to operands of type 'double' and 'double'

I'm using the ?? because there is a possiblity that the sub query will return no records. In that case the value should be zero.

I've tried casting the value to (double) and (double?) but I still get effectively the same error. j

I do a similar sub select on a column elsewhere in the statement an it works fine;

                  Consignment = (from x in Material_Locations
                                     where x.Material == j.Part_Number
                                         && x.Location_ID == "MSSICONSMT"
                                     select(float?) x.On_Hand_Qty ?? 0.0 ).ToList()

What am I missing here?

Thanks!

Bernie Hunt
  • 306
  • 4
  • 22
  • 1
    You are applying `??` to a row. If the query returns no rows it won't convert the absence of rows into a zero. – GSerg Dec 26 '17 at 19:58

1 Answers1

0

Thanks for the help finding references. I couldn't figure out how to google it.

This syntax seams to work.

                  on_Hand_Qty1 = Material_Locations
                                .Where (ml => ml.Material == j.Part_Number && ml.Location_ID != "MSSICONSMT" )
                                .Sum (ml => (double?)ml.On_Hand_Qty) ?? 0,
Bernie Hunt
  • 306
  • 4
  • 22