2012-04-19 110 views
3

我構建了一個簡單的查詢以返回用戶團隊成員資格(N:N關係)。這對所有用戶都適用,但是當我添加一個where子句來限制特定用戶時,它會拋出一個錯誤異常(請參閱下面的stacktrace)。Microsoft Dynamics 2011 N:N LINQ查詢與包含Guid的where子句

奇怪的是,這適用於「where Users.FullName.StartsWith(」Alex「)」。 Dynamics CRM SDK LINQ實現是否不支持where子句中的Guid?

有什麼建議嗎?

實施例代碼

using (var service = new OrganizationService("Xrm")) 
     { 
      using (var xrm = new XrmServiceContext(service)) 
      { 
       var AlexUser = xrm.SystemUserSet.Where(p => p.FullName.StartsWith("Alex")).First(); 
       var AlexID = AlexUser.Id; 

       var Test = 
         from Users in xrm.SystemUserSet 
         join TeamMemberships in xrm.TeamMembershipSet on Users.Id equals TeamMemberships.SystemUserId 
         join Teams in xrm.TeamSet on TeamMemberships.TeamId equals Teams.Id 
         where Users.Id == AlexID  // <-- problematic where clause 
         orderby Users.FullName 
         select new 
         { 
          FullName = Users.FullName, 
          UserID = Users.Id, 
          TeamName = Teams.Name 
         }; 

       var Test1 = Test.ToList(); 
      } 
     } 

堆棧跟蹤

服務器堆棧跟蹤:在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime 操作,ProxyRpc & RPC)在 System.ServiceModel.Channels.ServiceChannel.Call(String action, 布爾單向,ProxyOperationRuntime操作中,在 System.ServiceModel.Channels.ServiceChannel.Call(字符串動作對象[]插件, 對象[]奏,時間跨度超時), 布爾單向,ProxyOperationRuntime操作,對象[]插件, 對象[]奏)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 包括methodCall,ProxyOperationRuntime操作)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(即時聊天 消息)在

異常重新拋出[0]:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,即時聊天retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData & MSGDATA,的Int32類型)處 Microsoft.Xrm Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest 請求)。 Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest 請求)處 Microsoft.Xrm.Client.Services.OrganizationService Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest 請求)。 <> C_ DisplayClass19.b _18在 微軟(IOrganizationService 多個)在 Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService [TResult](Func鍵2 action) at Microsoft.Xrm.Client.Services.OrganizationService.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Linq.QueryProvider.RetrieveEntityCollection(OrganizationRequest request, NavigationSource source) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute(QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, List 1 linkLookups, 字符串& pagingCookie,布爾& moreRecords) .Xrm.Sdk.Linq.QueryProvider.Execute [TElement](QueryExpression QE,布爾throwIfSequenceIsEmpty,布爾throwIfSequenceNotSingle, 投影投影,NavigationSource源,列表1 linkLookups)
at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](Expression expression) at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetEnumerator[TElement](Expression expression) at Microsoft.Xrm.Sdk.Linq.Query
1.GetEnumerator()在 System.Collections.Generic.List 1..ctor(IEnumerable 1個集合)
at System.Linq.Enumerable.ToList [TSource](IEnumerable`1 source)at aspirets.crm.test.Program.Main(String [] args)in C:\ Users \ a_marshall \ documents \ visual studio 2010 \ Projects \ aspirets.crm \ aspirets.crm.test \ Program.cs:第37行,在 System.AppDomain。_nExecuteAssembly(RuntimeAssembly組件,字符串[] 參數)在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在的System.Threading在System.AppDomain.ExecuteAssembly(字符串assemblyFile, 證據assemblySecurity,字串[] args)。 ThreadHelper.ThreadStart_Context(對象狀態)
在System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback回調,對象的狀態,布爾 ignoreSyncCtx)在 System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback回調,對象狀態)在 System.Threading.ThreadHelper.ThreadStart()

+2

不是'Users.Id',而是嘗試'Users.SystemUserId'。 – 2012-04-19 17:32:07

+0

同樣,不是'Teams.Id',而是嘗試'Teams.TeamId'。 – 2012-04-19 17:34:04

+0

我假設這工作? – 2012-04-23 14:24:43

回答

5

而不是Users.Id,請嘗試Users.SystemUserId。同樣,代替Teams.Id,請嘗試Teams.TeamId

至於爲什麼這樣工作的原因,我不知道任何說明這一點的文檔,但因爲生成的早期綁定文件中的實體繼承自Entity,它們必然具有Id屬性。但是,因爲早期綁定的OrganizationServiceContext將實體屬性直接映射到CRM數據庫,其中的表不包含Id列,因此在LINQ提供程序中使用Id屬性將不起作用,因此您必須使用實際的數據庫/模式名稱。