2014-10-29 71 views
0

我有一個在「預驗證」中註冊的Dynamics CRM插件並在刪除時觸發。 我在裏面有一個LINQ查詢,它檢索特定父記錄的子記錄的日期字段的最大值。動態CRM插件 - 預先驗證中的LINQ查詢

這裏是我的代碼:

var q = (from e1 in serviceContext.CreateQuery<entity1>() 
     join e2 in serviceContext.CreateQuery<entity2>() on e1.typeid.Id equals e2.codeId 
     where e1.statecode == 0 && e1.ParentId.Id.Equals(new Guid(ParentGuidStr)) 
     orderby e1.dt descending 
     select new {e1.dt, e2.code}).ToList(); 

我收到上述查詢以下錯誤,當該插件觸發上的記錄是無效的:

PreValidateEntity1Delete PlugIn Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException: Value cannot be null. Parameter name: g

我不知道爲什麼我我正在獲取上述錯誤,並且如果LINQ查詢中存在非活動子記錄和父記錄之間的鏈接,或者存在其他原因。

回答

2

首先,請確保所有子女記錄都有父母記錄的參考。 然後,改變你在那裏contidion到:

where e1.statecode == 0 && e1.ParentId != null && e1.ParentId.Id.Equals(new Guid(ParentGuidStr)) 

的問題是,爲EntityReference場(e1.ParentId),可以爲null,當您試圖訪問一個空實體時,會出現錯誤。

因此,您還應該確保e1.typeid在連接條件中也不爲空。

或者,您可以嘗試使用2個獨立查詢的解決方法,並從結果中收集信息。

祝你好運!