4

我构建了一个简单的查询来返回用户团队成员资格(N:N 关系)。这对所有用户都适用,但是,当我添加 where 子句以限制特定用户时,它会引发错误异常(请参阅下面的堆栈跟踪)。

奇怪的是,这适用于“where Users.FullName.StartsWith("Alex")”。Dynamics CRM SDK LINQ 实施是否不支持 where 子句中的指南?

有什么建议吗?

示例代码

 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 operation, ProxyRpc& rpc) 在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs , TimeSpan timeout) 在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation ) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

在 [0] 处重新引发异常:在 Microsoft.Xrm 的 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 处的 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)。 Sdk.IOrganizationService.Execute(OrganizationRequest request) 在 Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest request) 在 Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest request) 在 Microsoft.Xrm.Client.Services。 Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService[TResult](Func2 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, List1 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.QueryMicrosoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, List 1.GetEnumerator() at System.Collections 1 linkLookups, String& pagingCookie, Boolean& moreRecords) .Generic.List 1..ctor(IEnumerable1 个集合)
在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at aspirets.crm.test.Program.Main(String[] args) 在 C:\Users\a_marshall\documents\visual studio 2010\Projects\aspirets .crm\aspirets.crm.test\Program.cs:第 37 行 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft .VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart()

4

1 回答 1

6

而不是Users.Id,尝试Users.SystemUserId。同样,代替Teams.Id,尝试Teams.TeamId

至于这样做的原因,我不知道有任何说明这一点的文档,但是由于生成的早期绑定文件中的实体继承自Entity,它们必然具有Id属性。但是,由于早期绑定OrganizationServiceContext将实体属性直接映射到 CRM 数据库,其中不包含Id列的表,使用Id带有 LINQ 提供程序的属性将不起作用,因此您必须使用实际的数据库/架构名称。

于 2012-04-24T14:07:20.443 回答