Dynamics CRM SDK-使用OrganizationServiceContext的linq的IN运算符 [英] Dynamics CRM SDK - IN operator for linq with OrganizationServiceContext

查看:241
本文介绍了Dynamics CRM SDK-使用OrganizationServiceContext的linq的IN运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用svcutil生成的OrganizationServiceContext实现从CRM检索实体:

I'm using my OrganizationServiceContext implementation generated by the svcutil to retrieve entities from CRM:

context.new_productSet.First(p => p.new_name == "Product 1");

是否可以一次检索具有不同属性值的多个实体-(有点像SQL中的IN运算符)?

Is it possible to retrieve multiple entities with different attribute values at once - (smth like IN operator in SQL)?

示例:我想通过一个调用来检索多个产品(产品1,产品2,...)。产品名称列表是动态的,存储在称为productNames的数组中。

Example: I would like to retrieve multiple products ("Product 1", "Product 2", ...) with a single call. The list of product names is dynamic, stored in an array called productNames.

推荐答案

不,您不能。 CRM LINQ提供程序仅允许变量出现在表达式的左侧,而右侧必须包含常量。

No, you can't. CRM LINQ provider only allows variables to appear on the left side of expressions, while the right side must contain constants.

ie

Product.Where(e => e.Name == desiredName)

不受支持且无法正常工作(它会抱怨在比较右侧使用变量)。

Is not supported and won't work (it will complain about using a variable on the right side of the comparison).

如果不能避免这种查询,您必须先 .ToList()数据(这可能导致巨大的结果集,并且可能会导致会令人难以置信地慢):

If you cannot avoid this kind of query, you have to .ToList() data first (this can lead to a huge result set and will probably turn up to be unconceivably slow):

Product.ToList().Where(e => e.Name == desiredName)

这将起作用,因为现在 .Where()而是应用于 List<>

This will work, because now the .Where() is being applied on a List<> instead.

另一种方法(我没有关于性能)将创建多个查询,基本上一次获取一个记录:

Another approach (I don't have data about performance, though) would be to create many queries, basically fetching the records one at a time:

// ... this is going to be a nightmare ... don't do it ...
var entities = new List<Product>();
entities.Add(Product.Where(e => e.Name == "Product 1"));
entities.Add(Product.Where(e => e.Name == "Product 2"));

或者使用 QueryExpression 这样(我个人喜好,因为我总是迟到)

Or use a QueryExpression like this (my personal favourite, because I always go late-bound)

var desiredNames = new string[]{"Product 1", "Product 2"};
var filter = new FilterExpression(LogicalOperator.And)
{
    Conditions = 
    {
        new ConditionExpression("name", ConditionOperator.In, desiredNames)
    }
};
var query = new QueryExpression(Product.EntityLogicalName)
{
    ColumnSet = new ColumnSet(true),
    Criteria = filter
};
var records = service.RetrieveMultiple(query).Entities;

这篇关于Dynamics CRM SDK-使用OrganizationServiceContext的linq的IN运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆