Linq to CRM (early binding): the join operator throws an exception when the lambda expression is not

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)

+6
source share
1 answer

Where statements from different entities should be entered in the split where statements are .

The where clause applies a filter to the results, often using a Boolean expression. The filter determines which items to exclude from the original sequence. Each where clause can contain only conditions for one type of entity. The composite condition for using multiple objects is invalid. Instead, each organization should be filtered in a separate place where the items.

The following should probably take care of this.

 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; } } 
+7
source

Source: https://habr.com/ru/post/899475/


All Articles