gpt4 book ai didi

c# - 如何检索到可查询成员的绑定(bind)?

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:14 25 4
gpt4 key购买 nike

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

this._xrmServiceContext.AccountSet.FirstOrDefault(x => x.Id == someGuid);

但是,在编译期间我们不知道它是 AccountSet、AccountLeadSet 还是其他 500 个可查询对象。

我想要有将绑定(bind)返回到 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>();
}
}
}

如何查询 AccountSet 或 ActivityPointerSet 或任何其他集合,在运行时只知道集合的名称

最佳答案

听起来你在寻找 .CreateQuery(),看看这个示例(来自 here ):

var xrmServiceContext = new OrganizationServiceContext(_service);

var lstContact = (from c in xrmServiceContext.CreateQuery("contact")
where c["firstname"] == "Hugh"
select c).ToList();

foreach (var contact in lstContact)
{
MessageBox.Show(contact["fullname"].ToString());
}

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

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

关于c# - 如何检索到可查询成员的绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42349449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com