Query Datatable with LINQ
-
Tuesday, November 13, 2012 5:38 PM
Hi,
I'm doing a query in my c# code behind to a datatable with the following objects:
Datatable: tableLostOpp
Columns: Age(string) and Price(decimal)
tableLostOpp.AsEnumerable().Where(y => y.Field<string>("Age") == "2").Sum(x => x.Field<decimal>("Price"))
I want to sum all Price values where column Age has value 2.
The big problem is that I have some rows with Age = 2 and no Price value (null), and I get and error of conversion decimal to null.
Cannot cast DBNull.Value to type 'System.Decimal'. Please use a nullable type.
How can I solve this? Can I put a condition for non nulls in where clause for price? I can't achieve it?
Thanks
All Replies
-
Tuesday, November 13, 2012 5:48 PM
Friend,
Please try as below.tableLostOpp.AsEnumerable().Where(y => y.Field<string>("Age") == "2").Sum(x => x.Field<decimal?>("Price"))
-- Thanks Ajith R Nair
- Marked As Answer by HugoDias Tuesday, November 13, 2012 7:17 PM
-
Tuesday, November 13, 2012 7:26 PM
You were wrigth, with the ? accepts the nulls.
Thank you very much!

