C# 如何检索到可查询成员的绑定?

C# 如何检索到可查询成员的绑定?,c#,.net,visual-studio,linq,dynamics-crm-2011,C#,.net,Visual Studio,Linq,Dynamics Crm 2011,通常,我可以这样查询下面的类: this._xrmServiceContext.AccountSet.FirstOrDefault(x => x.Id == someGuid); 然而,在编译期间,我们不知道它是AccountSet、AccountLeadSet还是其他500个查询文件 我希望具有将返回绑定到XrmServiceContext的功能,例如: _xrmServiceContextBind.Where(var querable => iquerableName == n

通常,我可以这样查询下面的类:

this._xrmServiceContext.AccountSet.FirstOrDefault(x => x.Id == someGuid);
然而,在编译期间,我们不知道它是AccountSet、AccountLeadSet还是其他500个查询文件

我希望具有将返回绑定到XrmServiceContext的功能,例如:

_xrmServiceContextBind.Where(var querable => iquerableName == name + "Set").FirstOrDefault(x => x.Id == someGuid)
XrmServiceContext的定义如下:

public partial class XrmServiceContext : Microsoft.Xrm.Sdk.Client.OrganizationServiceContext
    {

        /// <summary>
        /// Constructor.
        /// </summary>
        public XrmServiceContext(Microsoft.Xrm.Sdk.IOrganizationService service) : 
                base(service)
        {
        }

        /// <summary>
        /// Gets a binding to the set of all <see cref="Xrm.Account"/> entities.
        /// </summary>
        public System.Linq.IQueryable<Xrm.Account> AccountSet
        {
            get
            {
                return this.CreateQuery<Xrm.Account>();
            }
        }

        /// <summary>
        /// Gets a binding to the set of all <see cref="Xrm.AccountLeads"/> entities.
        /// </summary>
        public System.Linq.IQueryable<Xrm.AccountLeads> AccountLeadsSet
        {
            get
            {
                return this.CreateQuery<Xrm.AccountLeads>();
            }
        }

        /// <summary>
        /// Gets a binding to the set of all <see cref="Xrm.ActivityMimeAttachment"/> entities.
        /// </summary>
        public System.Linq.IQueryable<Xrm.ActivityMimeAttachment> ActivityMimeAttachmentSet
        {
            get
            {
                return this.CreateQuery<Xrm.ActivityMimeAttachment>();
            }
        }

        /// <summary>
        /// Gets a binding to the set of all <see cref="Xrm.ActivityParty"/> entities.
        /// </summary>
        public System.Linq.IQueryable<Xrm.ActivityParty> ActivityPartySet
        {
            get
            {
                return this.CreateQuery<Xrm.ActivityParty>();
            }
        }

        /// <summary>
        /// Gets a binding to the set of all <see cref="Xrm.ActivityPointer"/> entities.
        /// </summary>
        public System.Linq.IQueryable<Xrm.ActivityPointer> ActivityPointerSet
        {
            get
            {
                return this.CreateQuery<Xrm.ActivityPointer>();
            }
        }
}
公共部分类XrmServiceContext:Microsoft.Xrm.Sdk.Client.OrganizationServiceContext { /// ///构造器。 /// 公共XrmServiceContext(Microsoft.Xrm.Sdk.IOR组织服务): 基地(服务) { } /// ///获取到所有实体集的绑定。 /// public System.Linq.IQueryable帐户集 { 收到 { 返回这个.CreateQuery(); } } /// ///获取到所有实体集的绑定。 /// public System.Linq.IQueryable AccountLeadsSet { 收到 { 返回这个.CreateQuery(); } } /// ///获取到所有实体集的绑定。 /// public System.Linq.IQueryable活动时间附件集 { 收到 { 返回这个.CreateQuery(); } } /// ///获取到所有实体集的绑定。 /// public System.Linq.IQueryable ActivityPartySet { 收到 { 返回这个.CreateQuery(); } } /// ///获取到所有实体集的绑定。 /// public System.Linq.IQueryable活动集 { 收到 { 返回这个.CreateQuery(); } } }
如何查询AccountSet、ActivityPointSet或任何其他集合,在运行时只知道集合的名称?

听起来像是在查找
.CreateQuery()
,请查看此示例(来自):

在您的场景中,它看起来像:

_xrmServiceContextBind.CreateQuery($"{iquerableName.ToLower()}").FirstOrDefault(x => x.Id == someGuid)

听起来您在寻找
.CreateQuery()
,请查看此示例(来自):

在您的场景中,它看起来像:

_xrmServiceContextBind.CreateQuery($"{iquerableName.ToLower()}").FirstOrDefault(x => x.Id == someGuid)

虽然Alex为您的问题提供了答案,但我有一个基本问题,就是如何将一个看起来相当丑陋的LINQ查询拼凑在一起,而不提供早期绑定类型检查的好处(请不要误解我的意思,这只是一个您无法使用它的情况)

如果您在Prem上运行CRM,或者在CRM online上运行CRM,并且您愿意使用IlMege,那么我建议您使用DLaB.Xrm Nuget包(完全公开,我写了它)。它为您提供了扩展方法来编写查询,如下所示:

使用DLaB.Xrm

var entityLogicalName = getName();
var entity = this._xrmService.GetEntitiesById(entityLogicalName, someGuid).FirstOrDefault();

没有真正奇怪的LINQ语句需要编写/维护,只有一行代码可以满足您的需要。

虽然Alex提供了您问题的答案,但我有一个基本问题,就是如何将一个看起来相当难看的LINQ查询拼凑在一起,而不提供早期绑定类型检查的好处(别误会我的意思,这只是一个你不能使用它的情况。)

如果您在Prem上运行CRM,或者在CRM online上运行CRM,并且您愿意使用IlMege,那么我建议您使用DLaB.Xrm Nuget软件包(完全公开,我编写了该软件包)。它为您提供了编写查询的扩展方法,如下所示:

使用DLaB.Xrm

var entityLogicalName = getName();
var entity = this._xrmService.GetEntitiesById(entityLogicalName, someGuid).FirstOrDefault();
没有真正奇怪的LINQ语句需要编写/维护,只有一行代码可以满足您的需要