I use the following code in wcf ria services (Silverlight App):
public partial class BillingWaterDomainService : LinqToEntitiesDomainService<BilingWaterEntities>
{
public ObservableCollection<PaymentSummary> GetPaymentSummary(long requestId)
{
var paymentSummaries = new ObservableCollection<PaymentSummary>();
var result = GetRequestCostDetailsByRequestId(requestId);
foreach (var requestCostDetail in result.Where(r=>r.BranchCostId.HasValue))
{
if (requestCostDetail.Debtor.HasValue)
{
RequestCostDetail detail = requestCostDetail;
long? costCustomerPrice = 0;
costCustomerPrice =
result.Where(
r => r.CostCustomerDetail.CostCustomerType.CostTypeId == detail.BranchCostDetail.CostType && r.Creditor.HasValue).
Sum(r => r.Creditor != null ? r.Creditor.Value : 0);
paymentSummaries.Add(new PaymentSummary()
{
PaymentTitle = requestCostDetail.BranchCostDetail.CostType1.CostTitle,
Price = requestCostDetail.Debtor.Value-(costCustomerPrice.HasValue ? costCustomerPrice.Value:0)
});
}
}
return paymentSummaries;
}
}
When i try execute this code, i have following error:
Invoke Operation
‘GetPaymentSummary’ failed.The cast to value type ‘Int64’ failed
because the materialized value is null.Either the result type ‘s
generic parameter or the query must use a nullable type
this error in the following line of code:
costCustomerPrice =result.Where(
r => r.CostCustomerDetail.CostCustomerType.CostTypeId == detail.BranchCostDetail.CostType && r.Creditor.HasValue).
Sum(r => r.Creditor != null ? r.Creditor.Value : 0);
And This piece of code:
r => r.CostCustomerDetail.CostCustomerType.CostTypeId
how can i solve this problem?!
It seems that
r => r.CostCustomerDetail.CostCustomerType.CostTypeIdis null for some entries. Try adding a condition:Or maybe even
r.CostCustomerDetail.CostCustomerTypeis sometimes null.You can also do this: