20

I am having a problem with this query it's throwing an error.

var TotalToDatePayable = (  from ori in db.GetAll<WMPORI>()
                           where ori.CTMSysID == ctmSysId
                          select ori.ExB4Taxes).Sum();

I tried below code from another similar question but that did not solve my problem:

 var TotalToDatePayable = (Decimal?)(  from ori in db.GetAll<WMPORI>()
                                      where ori.CTMSysID == ctmSysId
                                     select ori.ExB4Taxes).Sum()) ?? 0;
Community
  • 1
  • 1
Developer
  • 2,987
  • 10
  • 40
  • 51
  • This is working: var TotalToDatePayable = (from ori in db.GetAll() where ori.CTMSysID == ctmSysId select ori.ExB4Taxes).DefaultIfEmpty(0).Sum(); – Developer May 30 '12 at 16:18
  • @DmitryBychenko I thought that kind of indenting was exclusive to Objective C. Now I'm sad. – Rawling Jun 26 '15 at 07:52
  • @Rawling 9: this kind of indentation is typical in SQL (which Linq emulates in the question) as well. That was the reason for me to add spaces. – Dmitry Bychenko Jun 26 '15 at 07:58

1 Answers1

35

You need to cast ori.ExB4Taxes to decimal? inside the query.

var TotalToDatePayable = (from ori in db.GetAll<WMPORI>()
                          where ori.CTMSysID == ctmSysId
                          select (Decimal?) ori.ExB4Taxes).Sum() ?? 0;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • You only need to add 1 more Parentheses at the beginning of query other than that its working perfect. Thanks. – Developer May 30 '12 at 16:35