简体   繁体   English

Xrm.Sdk Linq with Joins and dynamic where

[英]Xrm.Sdk Linq with Joins and dynamic where

After a bunch of time getting my query syntactically correct it looks like the the sdk has some limitations.. i also tried a sub query with facility but it returned a IQuery (i or something).经过一段时间让我的查询在语法上正确无误,sdk 似乎有一些限制。 Is there a way to achieve a dynamic where clause in a Linq Xrm Query有没有办法在 Linq Xrm 查询中实现动态 where 子句

the exception thrown抛出的异常

System.NotSupportedException: The method 'Where' cannot follow the method 'Select' or is not supported. System.NotSupportedException:方法“Where”不能遵循方法“Select”或不受支持。 Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' m ethod before calling unsupported methods.尝试根据支持的方法编写查询,或者在调用不受支持的方法之前调用“AsEnumerable”或“ToList”方法。

var predicate = PredicateBuilder.New<GetAllResult>(false);

if (query?.ObservationOnStart != null && query?.ObservationOnEnd != null)
{
    predicate.And(result =>
        result.ObservationEntity.esor_ObservationOn >= query.ObservationOnStart &&
        result.ObservationEntity.esor_ObservationOn <= query.ObservationOnEnd);
}


var queryable = from observationEntity in _ctx.esor_ObservationSet
        join facilityEntity in _ctx.core_FacilitySet
            on observationEntity.esor_Facility.Id equals facilityEntity.Id
        orderby observationEntity.esor_ObservationOn
        select new GetAllResult {ObservationEntity = observationEntity, FacilityEntity = facilityEntity}
    ;


// THIS throws exception
 queryable.Where(predicate).ToList(); 

I have also tried checking the a varable and used and OR but it also throws an exception我也试过检查变量和使用的和或但它也抛出异常

where completedById.Equals(null) || observationEntity.esor_CompletedBy.Id.Equals(completedById)

System.NotSupportedException: Invalid 'where' condition. System.NotSupportedException: 无效的“where”条件。 An entity member is invoking an invalid property or method实体成员正在调用无效的属性或方法

In your first code snippet, you first call:在您的第一个代码片段中,您首先调用:

select new GetAllResult 

and then进而

queryable.Where(predicate) 

which doesn't work.这不起作用。 because - as you've find out - you can't call where after calling select .因为 - 正如你所发现的 - 在调用select之后你不能调用where

Then, in your second code snippet, you call:然后,在您的第二个代码片段中,您调用:

where completedById.Equals(null)

Which doesn't work because the left hand side of the where clause has to be an entity attribute name, while completedById is apparently a variable you've delcared somewhere.这不起作用,因为where子句的左侧必须是一个实体的属性名称,而completedById显然你已经delcared地方的变量。

CRM Dynamics's Linq queries must be kept pretty simple, and if you insist on using Linq (and not, say, QueryExpression ), do as follows: CRM Dynamics 的 Linq 查询必须保持非常简单,如果您坚持使用 Linq(而不是QueryExpression ),请执行以下操作:

// ... build you queryable, and then:

// 1. first, call ToList()
var records = queryable.ToList();

// 2. then, filter it with Where()
if (query?.ObservationOnStart != null && query?.ObservationOnEnd != null)
{
    records = records.Where(r =>
        r.ObservationEntity.esor_ObservationOn >= query.ObservationOnStart &&
        r.ObservationEntity.esor_ObservationOn <= query.ObservationOnEnd
    ).ToList();
}

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

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