Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Coalescence blues
Message
De
15/03/2017 05:12:52
John Baird
Coatesville, Pennsylvanie, États-Unis
 
 
À
15/03/2017 04:15:29
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 5.0
OS:
Windows 8.1
Network:
Windows Server 2012 R2
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01649038
Message ID:
01649082
Vues:
33
>Ah. I was thinking that Weight itself was nullable. Try this:
>
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;
>
>
>
>>Hi Viv.
>>
>>>If you want null results to be treated as 0 then you could just use : Sum(r=>r.Weight ?? 0)
>>
>>Doesn't compile either.
>>
>>This on the other hand does work. Interestingly q does not return null if there no transactions in EM. So why won't it sum()?
>>
>>
>>        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.

Try
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
.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform