var q = context.EMTransactions.Where(r => r.TransactionHeaders_Id == transactionHeaders_Id).Sum(r => (decimal?) r.Weight); >// or, if you don't want q to be nullable: >var q = context.EMTransactions.Where(r => r.TransactionHeaders_Id == transactionHeaders_Id).Sum(r => (decimal?) r.Weight) ?? 0; >>
>> public static decimal ComputeWeightDifference(int transactionHeaders_Id,PICSEntities context) >> { >> decimal emWeight = 0; >> var q = context.EMTransactions.Where(r => (r.TransactionHeaders_Id == transactionHeaders_Id)); >> if ((q.Count() == 0) == false) { emWeight = q.Sum(r => r.Weight); } >> decimal detailsWeight = context.TransactionDetails.Where(r => (r.TransactionHeaders_Id == transactionHeaders_Id)).Sum(r => r.Weight); >> return emWeight - detailsWeight; >> } >>The sum statement won't evaluate until its called by some means.
decimal detailsWeight = context.TransactionDetails.Where(r => (r.TransactionHeaders_Id == transactionHeaders_Id)).Sum(r => r.Weight).ToList();
The .ToList() will force the evaluation of the linq query