简体   繁体   English

LINQ to Dynamics CRM查询优化

[英]LINQ to Dynamics CRM query optimisation

I'm working with a website that links into a Dynamics CRM Online. 我正在使用链接到Dynamics CRM Online的网站。 I'm new to both of these but find the best way to learn is to put yourself under pressure. 我对这两者都不熟悉,但找到最好的学习方法就是让自己承受压力。

Anyway, I have the following LINQ query that I've built using LinqPad: 无论如何,我有使用LinqPad构建的以下LINQ查询:

from m in py3_membershipSet
join c in ContactSet on m.py3_Member.Id equals c.ContactId 
where m.statuscode.Value == 1
orderby m.py3_name
select m

However, this gives an out of memory exception. 但是,这会给出内存不足的异常。 It runs ok if I use Take(100) but I expect there to be about 1200 results to retrieve in total. 如果我使用Take(100)它运行正常但我希望总共可以检索大约1200个结果。 Whether the memory issue is a LinqPad related problem I don't know but either way, I am assuming the above query isn't the most efficient way to pull these results. 内存问题是否是LinqPad相关的问题,我不知道,但无论哪种方式,我假设上述查询不是拉这些结果的最有效方法。

I could really do with some help on making this more efficient, if it is as much of a memory hog as it appears via LinqPad. 我真的可以帮助提高效率,如果它是通过LinqPad出现的内存耗费。

An OutOfMemory exception , OutOfMemory异常

...is thrown when there is not enough memory to continue the execution of a program. 当没有足够的内存来继续执行程序时抛出....

So I don't think it is anything in particular with the Linq you have written - apart from that it returning more data than your client can cope with. 所以我认为你所编写的Linq并没有特别之处 - 除了它返回的数据多于客户可以处理的数据。 I suspect this is an issue more to do with your client than CRM or Linq. 我怀疑这是一个与客户有关的问题,而不是CRM或Linq。

This might be something do with LinqPad (not used it myself), have you tried running that script from a console app (to rule out any LinqPad issues)? 这可能与LinqPad有关(我自己没用过),你试过从控制台应用程序运行该脚本(以排除任何LinqPad问题)吗?

1200 doesn't sound like and awful lot of data, I often retrieve 1000~ records without issue but I have happily retrieved far more (5000~). 1200听起来不像和可怕的数据,我经常检索1000~记录没有问题,但我很高兴检索到更多(5000〜)。

Paging might avoid the problem; 分页可能会避免这个问题; Page Large Result Sets with LINQ . 使用LINQ的页面大结果集

Related reading: Troubleshooting Exceptions: System.OutOfMemoryException 相关阅读: 故障排除异常:System.OutOfMemoryException

Because the query does not know what fields will be needed later, all columns are returned from the entity when only the entity is specified in the select clause. 由于查询不知道以后将需要哪些字段,因此当只在select子句中指定实体时,将从实体返回所有列。 In order to specify only the fields you will use, you must return a new object in the select clause, specifying the fields you want to use. 要仅指定要使用的字段,必须在select子句中返回一个新对象,指定要使用的字段。

So instead of this: 所以不是这样的:

from m in py3_membershipSet
join c in ContactSet on m.py3_Member.Id equals c.ContactId 
where m.statuscode.Value == 1
orderby m.py3_name
select m

Use this: 用这个:

from m in py3_membershipSet
join c in ContactSet on m.py3_Member.Id equals c.ContactId 
where m.statuscode.Value == 1
orderby m.py3_name
select new py3_membership()
{
    py3_membershipid = m.py3_membershipid,
    py3_name = m.py3_name
}

Check out this post more details. 看看这篇文章的更多细节。

To Linq or not to Linq 要Linq还是不要Linq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM