当 lambda 表达式没有时,Linq to CRM(早期绑定(bind))join 语句会引发异常

标签 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;
    }
}

但奇怪的是,如果我将它重写为两个 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;
    }
}

异常(exception)是

System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: 'SystemUserRoles' entity doesn't contain attribute with Name = 'name'. (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).



堆栈跟踪是

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.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

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.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.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source) at CRM.Business.IntegrationServices.SystemUserService.IsSystemUserInRole(Guid systemUserId, String roleName, IOrganizationService service) at CRM.Plugin.OnExecute(IServiceProvider provider)

最佳答案

Where separate where statements 中需要引入来自不同实体的语句.

The where clause applies a filter to the results, often using a Boolean expression. The filter specifies which elements to exclude from the source sequence. Each where clause can only contain conditions against a single entity type. A composite condition involving multiple entities is not valid. Instead, each entity should be filtered in separate where clauses.



下面可能应该照顾它。

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

关于当 lambda 表达式没有时,Linq to CRM(早期绑定(bind))join 语句会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7796049/

相关文章:

无法从具有多个返回的 Select 中的用法推断出 C# 类型参数

c# - 从列表 2 中过滤列表 1

c# - 从电子邮件 C# 中获取收件人列表

indexing - 如何在 Crm 2011 上建立索引?

c# - 从字典中按标签获取按钮 c#

c# - 如何从 linq 查询中获取所有元素?

c# - 不支持 Linq to Entities SqlFunctions.DateDiff

未找到 C# 反射异常方法

javascript - 如何在 CRM Online 2013 中拥有唯一的 ID 号

javascript - 使用 javascript 调用外部 Web 服务 CRM 2011 RU 12 (Rollup 12)