Xrm.Sdk Linq with Joins and dynamic where
Xrm.Sdk Linq with Joins and dynamic where
经过一段时间让我的查询在语法上正确,看起来 sdk 有一些限制.. 我也尝试了一个带有 facility 的子查询,但它返回了一个 IQuery(i 或其他东西)。有没有办法在 Linq Xrm 查询中实现动态 where 子句
抛出异常
System.NotSupportedException: The method 'Where' cannot follow the method 'Select' or is not supported. Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' m
ethod before calling unsupported methods.
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();
我也试过检查变量并使用和 OR 但它也抛出异常
where completedById.Equals(null) || observationEntity.esor_CompletedBy.Id.Equals(completedById)
System.NotSupportedException: Invalid 'where' condition. An entity member is invoking an invalid property or method
在您的第一个代码片段中,您首先调用:
select new GetAllResult
然后是
queryable.Where(predicate)
这是行不通的。因为 - 正如您所发现的 - 您不能在调用 select
之后调用 where
。
然后,在您的第二个代码片段中,您调用:
where completedById.Equals(null)
这不起作用,因为 where 子句的左侧必须是实体属性名称,而 completedById
显然是您在某处指定的变量。
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();
}
经过一段时间让我的查询在语法上正确,看起来 sdk 有一些限制.. 我也尝试了一个带有 facility 的子查询,但它返回了一个 IQuery(i 或其他东西)。有没有办法在 Linq Xrm 查询中实现动态 where 子句
抛出异常
System.NotSupportedException: The method 'Where' cannot follow the method 'Select' or is not supported. Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' m ethod before calling unsupported methods.
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();
我也试过检查变量并使用和 OR 但它也抛出异常
where completedById.Equals(null) || observationEntity.esor_CompletedBy.Id.Equals(completedById)
System.NotSupportedException: Invalid 'where' condition. An entity member is invoking an invalid property or method
在您的第一个代码片段中,您首先调用:
select new GetAllResult
然后是
queryable.Where(predicate)
这是行不通的。因为 - 正如您所发现的 - 您不能在调用 select
之后调用 where
。
然后,在您的第二个代码片段中,您调用:
where completedById.Equals(null)
这不起作用,因为 where 子句的左侧必须是实体属性名称,而 completedById
显然是您在某处指定的变量。
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();
}