LINQ to CRM-或Where子句中 [英] LINQ to CRM - OR in Where clause

查看:93
本文介绍了LINQ to CRM-或Where子句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LINQ从Dynamics CRM 2011中引入一些数据.目标是使所有自某些日期以来发生更改的联系人记录,或具有自同一日期以来发生了更改的子实体(PERC文件)的所有联系人记录.查询如下所示:

I am trying to bring some data from Dynamics CRM 2011 using LINQ. The goal is to bring all Contact records that have changes since certain date OR have a child entity (PERC files) changed since that same date. The query looks like that:

// Bring all students who have changes (at Contact entity) after specific date
// momentInTime or the status of any of their perc files has been changed since
// that date 
var students = (from c in ContactSet
                join pl in cga_portallogonSet on c.Id equals pl.cga_ContactId.Id
                join ef in cga_percfileSet on c.Id equals ef.cga_StudentId.Id
                where
                    (pl.cga_PortalLogonRole.Value == 284970000) // student
                where
                    (c.ModifiedOn >= momentInTime || c.CreatedOn > momentInTime)
                    ||
                    (ef.cga_statuschangedate >= momentInTime)
                select c.cga_StudentNumber).Distinct().ToList();

这会产生以下错误消息:

This produces the following error message:

联系人"实体不包含名称为"cga_statuschangedate"的属性.

'Contact' entity doesn't contain attribute with Name = 'cga_statuschangedate'.

我无法弄清楚如何对两个不同的实体进行或"运算. MSDN说您需要每个实体的WHERE子句:

I cannot figure out how to do OR on two different entities. The MSDN says you need WHERE clause for each entity:

其中条款

为了过滤结果集,可以针对一个或多个>实体添加where子句.每个where子句只能包含针对单个实体类型的条件. >涉及多个实体的复合条件无效.相反,应在单独的where子句中过滤每个实体.

In order to filter the result set, where clauses can be added against one or more of the >entities. Each where clause may only contain conditions against an individual entity type. >A composite condition involving multiple entities is not valid. Instead, each entity >should be filtered in separate where clauses.

http://msdn.microsoft.com/en-us/library/ff681565.aspx

还有另一种方法可以实现我所需要的吗?

Is there another way of achieving what I need?

推荐答案

不幸的是,您无法在单个linq语句中实现所需的功能,因为他们使用的liunq提供程序会简化为fetchXML,而fetchXML不支持您使用的方案

Unfortunately you cannot achieve what you want in a single linq statement beacuse the liunq provider they use boils down to fetchXML, and fetchXML does not support the scenario you are using.

更多详细信息...提取为您提供条件在实体或链接实体内部的条件.这些条件元素不能具有其他链接实体(仅直接父实体或链接实体)的属性. 此处是引用此限制的许多Microsoft论坛帖子之一的fetchXML

More detail... Fetch gives you condition's inside of entity's or link-entity's. These condition elements cannot have attributes in them from other linked entities, only the direct parent entity or link-entity. Here is one of many microsoft forum posts referencing this limitation of fetchXML

可能不是您要找的答案,是吗?作为丑陋的替代方案,您可以运行两个单独的查询并在内存中进行过滤(这可能会对性能造成损害).更好的是,如果您是本地部署,则可以针对过滤后的视图编写一些sql.祝你好运.

Probably not the answer you were looking for, eh? As an ugly alternative you you can run two separate queries and filter in memory (as damaging as that may be to performance). Or better yet if you are an on-premise deployment you can write some sql against the filtered views. Good luck.

这篇关于LINQ to CRM-或Where子句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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