tags:

views:

486

answers:

1

I got this error when i use sum function in linq.

The cast to value type 'Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

GroupProduct.Where(a => a.Product.ProductID==1).Sum(Content => Content.Amount==null?0:Content.Amount),
+2  A: 

You could exclude at source?

var sum = GroupProduct.Where(a => a.Product.ProductID==1 && a.Amount != null)
            .Sum(a => (decimal)a.Amount);
Marc Gravell
Thanks but null condition is required on a.Product.ProductID==1 ..
malik
@malik - I don't understand; can you re-phrase that comment?
Marc Gravell
Thank you again:There are two things.when i use a.Product.ProductID the object is null, can i check it in linq query.
malik
I mean in lamda expression can i load the reference and can i handle null in linq query.
malik