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;
}
}
但奇怪的是,如果我将它重写为两个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;
}
}
例外是
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 operation,Object [] ins,Object [] outs,TimeSpan timeout) 在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作) 在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 type) 在Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest request) 在Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest请求) 在Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest request) 在Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) 在Microsoft.Xrm.Sdk.Linq.QueryProvider.RetrieveEntityCollection(OrganizationRequest请求,NavigationSource源) 在Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute(QueryExpression qe,Boolean throwIfSequenceIsEmpty,Boolean throwIfSequenceNotSingle,Projection projection,NavigationSource source,List
1 linkLookups, String& pagingCookie, Boolean& moreRecords) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, List
1 linkLookups) 在Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute [TElement](表达式表达式) 在Microsoft.Xrm.Sdk.Linq.QueryProvider.System.Linq.IQueryProvider.Execute [TResult](表达式表达式) 在System.Linq.Queryable.FirstOrDefault [TSource](IQueryable`1 source) 在CRM.Business.IntegrationServices.SystemUserService.IsSystemUserInRole(Guid systemUserId,String roleName,IOrganizationService service) 在CRM.Plugin.OnExecute(IServiceProvider提供商)
答案 0 :(得分:7)
Where
个语句。
where子句将过滤器应用于结果,通常使用a 布尔表达式。过滤器指定要排除的元素 来自源序列。每个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;
}
}