Microsoft Dynamics 2011 N:N LINQ查询,其中where子句包含Guid [英] Microsoft Dynamics 2011 N:N LINQ query with where clause containing Guid

查看:70
本文介绍了Microsoft Dynamics 2011 N:N LINQ查询,其中where子句包含Guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个简单的查询以返回用户团队成员身份(N:N关系).这对所有用户都适用,但是,当我添加where子句以限制特定用户时,它将引发错误异常(请参见下面的stacktrace).

I have constructed a simple query to return a Users Team membership (N:N relationship). This works fine for all users, however when I add a where clause to restrict to a specific user it throws a fault exception (see stacktrace below).

奇怪的是,这与"where Users.FullName.StartsWith("Alex")可以正常工作. Dynamics CRM SDK LINQ实施是否不支持where子句中的Guid?

Strangely this works fine with "where Users.FullName.StartsWith("Alex")". Does the Dynamics CRM SDK LINQ implementation not support Guids in where clauses?

有什么建议吗?

示例代码

 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();
            }
        }

Stacktrace :

服务器堆栈跟踪:位于 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(字符串操作, 布尔型单向,ProxyOperationRuntime操作,Object [] ins, Object [] out)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)位于 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

在[0]处抛出异常: System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32类型)位于 Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest 要求) Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest 要求) 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](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, 弦乐pagesCookie,布尔值和amp;更多记录),网址: Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute [TElement](QueryExpression qe,布尔throwIfSequenceIsEmpty,布尔throwIfSequenceNotSingle, 投影投影,NavigationSource源,List 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个集合)
在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)在 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()

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest request) at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest request) at Microsoft.Xrm.Client.Services.OrganizationService.<>c_DisplayClass19.b_18(IOrganizationService s) at 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, 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.Query
1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
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:line 37 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

推荐答案

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

Instead of Users.Id, try Users.SystemUserId. Similarly, instead of Teams.Id, try Teams.TeamId.

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

As for the reason why this works, I'm not aware of any documentation that states this, but because entities in the generated early-binding file inherit from Entity, they necessarily have an Id property. However, because the early-bound OrganizationServiceContext maps entity attributes directly to the CRM database, the tables of which don't contain an Id column, using the Id attribute with the LINQ provider won't work, so you'd have to use the actual database/schema names.

这篇关于Microsoft Dynamics 2011 N:N LINQ查询,其中where子句包含Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆