【问题标题】:Microsoft Dynamics 2011 N:N LINQ query with where clause containing GuidMicrosoft Dynamics 2011 N:N LINQ 查询,其中 where 子句包含 Guid
【发布时间】:2012-05-01 04:58:42
【问题描述】:

我构造了一个简单的查询来返回用户团队成员身份(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 操作,ProxyRpc& rpc) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串动作, Boolean oneway, ProxyOperationRuntime 操作, Object[] ins, Object[] 出局,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串动作, Boolean oneway, ProxyOperationRuntime 操作, Object[] ins, 对象 [] 出局)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime 操作)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 留言)

在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(消息数据& msgData,Int32 类型)在 Microsoft.Xrm.Sdk.IOrganizationService.Execute(组织请求 请求)在 Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(组织请求 请求)在 Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest 请求)在 Microsoft.Xrm.Client.Services.OrganizationService.c__DisplayClass19.b__18(IOrganizationService s) 在 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, String& pagingCookie, Boolean& moreRecords) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](QueryExpression qe,布尔 throwIfSequenceIsEmpty,布尔 throwIfSequenceNotSingle, 投影投影,NavigationSource 源,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.Query1.GetEnumerator() 在 System.Collections.Generic.List1..ctor(IEnumerable1 集合)
在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 源) 在 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 程序集,字符串 [] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, 证据 assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 试试Users.Id,而不是Users.SystemUserId
  • 类似地,试试Teams.Id,而不是Teams.TeamId
  • 嗨,彼得,非常感谢您的成功!如果您将此解决方案作为答案而不是评论发布,那么我会将问题标记为已接受。

标签: linq dynamics-crm dynamics-crm-2011


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多