Is there a bug in the Microsoft linq provider for CRM, or am I doing what linqToCrm does not support?
I have a simple function that determines if a user is assigned a role that does not work.
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; } }
But oddly enough, if I rewrote it as two lambda expressions, it works fine.
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; } }
The exception is
System.ServiceModel.FaultException`1 [Microsoft.Xrm.Sdk.OrganizationServiceFault]: the SystemUserRoles object does not contain an attribute with the name = 'name'. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).
and stack trace
Server stack trace: in System.ServiceModel.Channels.ServiceChannel.HandleReply (operation ProxyOperationRuntime, ProxyRpc & rpc) in System.ServiceModel.Channels.ServiceChannel.Call (String action, Boolean oneway, ProxyOperationRuntime operation Object,] outs, TimeSpan timeout) in System.ServiceModel.Channels.ServiceChannelProxy.InvokeService (IMethodCallMessageCall, ProxyOperationRuntime method) in System.ServiceModel.Channels.ServiceChannelProxy.Invoke (message with message)
Exception thrown at [0]: in System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage (IMessage reqMsg, IMessage retMsg) in System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke (MessageData & msgData in Microsoft Int32 .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.Sd .Client.OrganizationServiceContext.Execute (OrganizationRequest request) in Microsoft.Xrm.Sdk.Linq.QueryProvider.RetrieveEntityCollection (OrganizationRequest request, NavigationSource source) in Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute (QueryExpressleeeleleeeleeeleleejectioneeleleeeionleeeionleeeleleeeeleleeeeleleeeeionleeeeleleeeeleleeeeionleeeeleleeeeleleeeeleleeeeleleeune , projection projection, source NavigationSource, list to 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
moreRecords) at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute [TElement] (QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, List 1 linkLookups) in Microsoft.Xrm. Sdk.Linq.QueryProvider.Execute [TElement] (expression expression) in Microsoft.Xrm.Sdk.Linq.QueryProvider.System.Linq.IQueryProvider.Execute [TResult] (expression expression) in System.Linq.Queryable.FirstOrDefault [TSource] (source IQueryable`1) in CRM.Business.IntegrationServices.SystemUserService.IsSystemUserInRole (Guid systemUserId, String roleName, IOrganizationService service) in CRM.Plugin.OnExecute (IServiceProvider provider)