c# - 从 CRM 加载数据的性能问题

标签 c# performance linq web crm

目前,当我们从我们的网站查询 CRM 时,我们的网站面临响应时间慢(超过 1 分钟)的问题。我们通过网络服务使用 CRM 2011。我们调查时发现,时间都花在了查询CRM的点上。

我们已使用 CrmSvcUtil.exe 生成映射到 CRM 实体的代理类。然后我们创建一个上下文实例,并使用 C# 中的 LINQ 查询 CRM。

当我们查询时,我们使用 LINQ to CRM 加载我们的父对象,然后我们使用 LoadProperty 加载相关的子对象。

我想知道是否有人使用不同的 CRM 查询方法,以及您在实现过程中是否遇到过此类问题。

我在下面包含了一个简化的示例查询。

    public void SelectEventById(Guid id)
     {
            var crmEventDelivery = this.ServiceContext.EventDeliverySet.FirstOrDefault(eventDelivery => eventDelivery.Id == id);
            if (crmEventDelivery != null)
            {
              this.SelectCrmEventDeliveryWithRelationships(crmEventDelivery);
            }
    }

    private void SelectCrmEventDeliveryWithRelationships(EventDelivery crmEventDelivery)
     {
            // Loading List of Venue Delivery on parent crmEventDelivery thats been passed
            this.ServiceContext.LoadProperty(crmEventDelivery, Attributes.EventDelivery.eventdelivery_venuedelivery);

            foreach (var venueDelivery in crmEventDelivery.eventdelivery_venuedelivery)
            {
                 // Loading Venue on each Venue Delivery
                 ServiceContext.LoadProperty(venueDelivery, Attributes.VenueDelivery.venue_venuedelivery);
            }

            // Loading List of Session Delivery on parent crmEventDelivery thats been passed
            this.ServiceContext.LoadProperty(crmEventDelivery, Attributes.EventDelivery.eventdelivery_sessiondelivery);

            foreach (var sessionDelivery in crmEventDelivery.eventdelivery_sessiondelivery)
            {
              // Loading Presenters on each Session Delivery
              ServiceContext.LoadProperty(sessionDelivery, Attributes.SessionDelivery.sessiondelivery_presenterbooking);
            }
   }

最佳答案

就像其他答案中提到的那样,您的主要问题是网络服务调用的数量。没有人提到的是,您可以使用查询连接通过一次调用检索多个对象。所以你可以尝试这样的事情:

var query_join = (from e in ServiceContext.EventDeliverySet
                         join v in ServiceContext.VenueDeliverySet on e.EventDeliveryId equals v.EvendDeliveryId.Id
                         join vn in ServiceContext.VenueSet on v.VenueDeliveryId equals vn.VenueDeliveryId.Id
                         join s in ServiceContext.SessionDeliverSet on e.EventDeliveryId equals s.EvendDeliveryId.Id
                         where e.EventDeliveryId == id // *improtant (see below)
                         select new { EventDelivery = e, VenueDelivery = v, Venue = vn, SessionDeliver = s }).ToList();

然后你可以在 query_join 上运行一个 foreach 并将它们放在一起。

***重要:不要使用基本 Id 属性 (e.Id),坚持使用 e.EntityNameId.Value(不知道为什么,但我花了一段时间才弄明白。Id 返回默认 Guid值“00000..”)。

关于c# - 从 CRM 加载数据的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12361294/

相关文章:

针对 SQLite 数据库的 C# 与 Python 查询

c# - IEnumerable<IEnumerable<T>> 到 IEnumerable<T> 使用 LINQ

c# - 公开 WPF 用户控件的属性

c# - 在作为 Windows 服务托管的 WCF 类库项目中使用 SqlDependency

java - 在 Activity 中使用内部 BroadcastReceiver 时出现内存问题

c++ - 每次在循环中进行临时均匀随机分布的效率如何?

android - 对于 android 开发,我可以在 ImageView 上使用 JPG 图像而不是 PNG 图像吗?

c# - 动态创建匿名类型的属性

c# - 实体 - 两个连接表是什么类型

c# - 我怎样才能确保在应用程序关闭之前在我的单例中处理一个对象?