gpt4 book ai didi

c# - 获取所有业务单元

转载 作者:行者123 更新时间:2023-12-02 04:42:34 25 4
gpt4 key购买 nike

我正在尝试从 CRM 2013 中检索所有业务单位。

尝试了以下查询

        var query = _serviceContext.BusinessUnitSet
.Where(b => b.EntityState == 0)
.Select(x => new
{
Name = x.Name,
Id = x.Id
}
)
.ToList();

使用此查询我收到一条错误消息:

{System.ServiceModel.FaultCode} {attributeName} {System.Collections.Generic.SynchronizedReadOnlyCollection<System.ServiceModel.FaultReasonText>}

在谷歌搜索这个主题时,我找到了有关如何检索单个业务单位的信息(这似乎与检索“正常”实体不同),但没有找到有关如何获取所有业务单位的信息 (link)。

如能就如何检索所有业务部门提供任何帮助,我们将不胜感激。

最佳答案

使用 QueryExpression 试试这个

    public class BusinessUnit
{
public Guid Id { get; set; }
public String Name { get; set; }
}


public void GetAllBusinessUnits(Action<QueryExpression> queryModifier = null)
{

foreach (BusinessUnit m in RetrieveAllBusinessUnit(this.Service, 1000, queryModifier))
{

//Console.WriteLine(m.Name);
}
}


public static IEnumerable<BusinessUnit> RetrieveAllBusinessUnit(IOrganizationService service, int count = 1000, Action<QueryExpression> queryModifier = null)
{

QueryExpression query = new QueryExpression("businessunit")
{
ColumnSet = new ColumnSet("businessunitid", "name"),

PageInfo = new PagingInfo()
{
Count = count,
PageNumber = 1,
PagingCookie = null,
}
};

if (queryModifier != null)
{
queryModifier(query);
}

while (true)
{
EntityCollection results = service.RetrieveMultiple(query);

foreach (Entity e in results.Entities)
{
yield return new BusinessUnit()
{
Id = e.GetAttributeValue<Guid>("businessunitid"),
Name = e.GetAttributeValue<String>("name")
};
}

if (results.MoreRecords)
{
query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = results.PagingCookie;
}
else
{
yield break;
}
}
}

关于c# - 获取所有业务单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20513456/

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