当lambda表达式没有时,Linq to CRM(早期绑定)join语句抛出异常

Dar*_*ryl 6 linq dynamics-crm-2011

Microsoft linq to CRM提供程序中是否存在错误,或者我正在执行linqToCrm不支持​​的操作?

我有一个简单的函数,确定是否为用户分配了一个不起作用的角色.

public static bool IsSystemUserInRole(Guid systemUserId,
                                      string roleName,
                                      Microsoft.Xrm.Sdk.IOrganizationService service)
{
    using (var crmService = new CrmContext(service))
    {
        return (from sr in crmService.SystemUserRolesSet
                join r in crmService.RoleSet
                    on sr.RoleId.Value equals r.RoleId.Value
                where sr.SystemUserId.Value == systemUserId && r.Name == roleName
                select sr.SystemUserId).FirstOrDefault() != null;
    }
}
Run Code Online (Sandbox Code Playgroud)

但奇怪的是,如果我将它重写为两个lambda表达式,它可以正常工作.

public static bool IsSystemUserInRole(Guid systemUserId,
                                      string roleName,
                                      Microsoft.Xrm.Sdk.IOrganizationService service)
{
    using (var crmService = new CrmContext(service))
    {
        var role = crmService.RoleSet.FirstOrDefault(r => r.Name == roleName);
        return role != null 
                && crmService.SystemUserRolesSet.FirstOrDefault(
                    ur => ur.SystemUserId == systemUserId
                          && ur.RoleId == role.RoleId) != null;
    }
}
Run Code Online (Sandbox Code Playgroud)

例外是

System.ServiceModel.FaultException`1 [Microsoft.Xrm.Sdk.OrganizationServiceFault]:'SystemUserRoles'实体不包含Name ='name'的属性.(故障详细信息等于Microsoft.Xrm.Sdk.OrganizationServiceFault).

并且堆栈跟踪是

服务器堆栈跟踪:System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc和rpc)在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime操作,Object [] ins,Object [] out System.SServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)中的System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作),TimeSpan超时)

在[0]处重新抛出异常:位于Microsoft.Xrm的System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)的System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg).位于Microsoft.Xrm.Sdk.Client的Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest请求)的Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest请求)中的Sdk.IOrganizationService.Execute(OrganizationRequest请求). Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute(QueryExpression qe,Boolean throwIfSequenceIsEmpty,Boolean throwIfSequenceNotSingle,Projection projection 1 linkLookups, String& pagingCookie, Boolean& moreRecords) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, ListMicrosoft.Xrm.Sdk.Lin上的Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute [TElement](表达式表达式),NavigationSource源,List 1 linkLookups q.QueryProvider.System.Linq.IQueryProvider.Execute [TResult](表达式表达式)在System.Linq.Queryable.FirstOrDefault [TSource](IQueryable`1 source)at CRM.Business.IntegrationServices.SystemUserService.IsSystemUserInRole(Guid systemUserId,String CRM.Plugin.OnExecute(IServiceProvider提供程序)中的roleName,IOrganizationService服务)

Pet*_*eed 7

Where来自不同实体的陈述需要在单独的where语句中引入.

where子句通常使用布尔表达式对结果应用过滤器.过滤器指定要从源序列中排除的元素.每个where子句只能包含针对单个实体类型的条件.涉及多个实体的复合条件无效.相反,应该在单独的where子句中过滤每个实体.

下面应该可以照顾它.

public static bool IsSystemUserInRole(Guid systemUserId,
                                      string roleName,
                                      Microsoft.Xrm.Sdk.IOrganizationService service)
{
    using (var crmService = new CrmContext(service))
    {
        return (from sr in crmService.SystemUserRolesSet
                join r in crmService.RoleSet
                    on sr.RoleId.Value equals r.RoleId.Value
                where sr.SystemUserId.Value == systemUserId
                where r.Name == roleName
                select sr.SystemUserId).FirstOrDefault() != null;
    }
}
Run Code Online (Sandbox Code Playgroud)