2012-03-06 43 views
0

我使用WCF RIA服务(Silverlight应用程序)下面的代码:解决拉姆达铸造错误

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; 
     } 
    } 

当我尝试执行该代码,我有以下错误:

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

此错误在下面的代码行:

costCustomerPrice =result.Where(
          r => r.CostCustomerDetail.CostCustomerType.CostTypeId == detail.BranchCostDetail.CostType && r.Creditor.HasValue). 
          Sum(r => r.Creditor != null ? r.Creditor.Value : 0); 

这一段代码:

r => r.CostCustomerDetail.CostCustomerType.CostTypeId 

我该如何解决这个问题?

+1

“GetPaymentSummary”在哪里调用? – 2012-03-06 17:28:48

+1

哪条线路故障?请显示完整的堆栈跟踪。 – 2012-03-06 17:30:05

+0

我在客户端Silverlight应用程序中调用了'GetPaymentSummary'。 – 2012-03-06 17:33:50

回答

1

看来r => r.CostCustomerDetail.CostCustomerType.CostTypeId对于某些条目为空。尝试添加一个条件:

results.Where(r => r.CostCustomerDetail.CostCustomerType.CostTypeId.HasValue) 
    .Where(... 

或甚至可能r.CostCustomerDetail.CostCustomerType有时为空。

你也可以这样做:

... .Select(r => r.Creditor != null ? r.Creditor.Value : 0) 
     .DefaultIfEmpty(0).Sum() 
+0

请参阅我的编辑。 – 2012-03-07 14:18:41

+0

感谢您编辑您的答案。帮助过我! :) – 2012-03-07 16:45:05

0

在某些时候,您正在尝试呼叫GetPaymentSummary并将空参数传递给它。你说你从客户端调用它 - 不知何故,空值传入为requestId

您可能需要更改方法来接受long?,并最初检查它是否为HasValue - 这样您就可以处理不发生严重故障的错误输入。

+0

** RequestId **的值在此行中不是Null.Error:costCustomerPrice = result.Where( r => r.CostCustomerDetail.CostCustomerType.CostTypeId == detail.BranchCostDetail.CostType && r.Creditor.HasValue)。和(r => r.Creditor!= null?r.Creditor.Value:0); – 2012-03-06 17:55:12

0

Befor使用ToList方法数总和法。 如果你改变了这个代码:

costCustomerPrice = 
         result.Where(
          r => r.CostCustomerDetail.CostCustomerType.CostTypeId == detail.BranchCostDetail.CostType && r.Creditor.HasValue). 
          Sum(r => r.Creditor != null ? r.Creditor.Value : 0); 

这个代码:

costCustomerPrice = 
         result.Where(
          r => r.CostCustomerDetail.CostCustomerType.CostTypeId == detail.BranchCostDetail.CostType && r.Creditor.HasValue).ToList(). 
          Sum(r => r.Creditor != null ? r.Creditor.Value : 0); 

你的计划有任何错误跑!