2012-03-06 42 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); 

你的計劃有任何錯誤跑!