gpt4 book ai didi

c# - 使用 DateTime 过滤器构建 LINQ to Entities 表达式树

转载 作者:行者123 更新时间:2023-11-30 21:41:10 25 4
gpt4 key购买 nike

我正在尝试完成 Todd Sprang 提供的动态查询的实现,请参阅此 link .目前我正在尝试启用按 DateTime 类型的过滤,并且我正在努力使用“大于”和“小于”运算符来制作 lambda 表达式,因此我的 SQL 将显示为“WHERE someDate<='2017-04-27 00: 00:00' 和 someDate<='2017-04-27 23:59:59'。

我设法提取了一个运算符方法:

private static readonly MethodInfo DateTimeGreaterThanOrEqualMethod = typeof(DateTime).GetMethod("op_GreaterThanOrEqual",
BindingFlags.Static | BindingFlags.Public);
private static readonly MethodInfo DateTimeLessThanOrEqualMethod = typeof(DateTime).GetMethod("op_LessThanOrEqual",
BindingFlags.Static | BindingFlags.Public);

这是我的 DateTime lambda 过滤器方法:

    private static Expression<Func<TDbType, bool>> ApplyDateTimeCriterion<TDbType,
TSearchCriteria>(TSearchCriteria searchCriteria, PropertyInfo searchCriterionPropertyInfo,
Type dbType, MemberInfo dbFieldMemberInfo, Expression<Func<TDbType, bool>> predicate)
{
var searchDateTime = searchCriterionPropertyInfo.GetValue(searchCriteria) as DateTime?;
if (searchDateTime == null)
{
return predicate;
}
var valueDateMin = ((DateTime)searchDateTime).Date;
var valueDateMax = new DateTime(valueDateMin.Year, valueDateMin.Month, valueDateMin.Day, 23, 59, 59);

var dbTypeParameter = Expression.Parameter(dbType, @"x");
var dbFieldMember = Expression.MakeMemberAccess(dbTypeParameter, dbFieldMemberInfo);
var criterionConstantMin = new Expression[] { Expression.Constant(valueDateMin) };
var criterionConstantMax = new Expression[] { Expression.Constant(valueDateMax) };
//having problem down here, when trying to call static method which needs two parameters
var greaterThanCall = Expression.Call(dbFieldMember, DateTimeGreaterThanOrEqualMethod, criterionConstantMin);
var lambdaGreaterThan = Expression.Lambda(greaterThanCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;

var lessThanCall = Expression.Call(dbFieldMember, DateTimeLessThanOrEqualMethod, criterionConstantMax);
var lambdaLessThan = Expression.Lambda(greaterThanCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;

return predicate.And(lambdaGreaterThan).And(lambdaLessThan);
}

我在调用 Expression.Call 时遇到问题,因为 op_GreaterThanOrEqual 是一个带有 2 个参数的静态方法。也许我不应该调用 op_GreaterThanOrEqual,也许这必须以其他方式完成?

最佳答案

I am having problem on calling Expression.Call because op_GreaterThanOrEqual is a static method with 2 parameters

Expression.Call 有几个重载,您需要对静态方法使用正确的重载(当前您使用的是带有 instance 参数的重载,因此是实例方法) .

也不要创建不必要的Expression[](接收多个表达式的方法通常使用params Expression[],因此会在需要时为您创建) :

var criterionConstantMin = Expression.Constant(valueDateMin);
var criterionConstantMax = Expression.Constant(valueDateMax);

这是有问题的电话

var greaterThanCall = Expression.Call(DateTimeGreaterThanOrEqualMethod, dbFieldMember, criterionConstantMin);
var lessThanCall = Expression.Call(DateTimeLessThanOrEqualMethod, dbFieldMember, criterionConstantMax);

maybe this has to be done in another way?

有特定的Expression 方法,如GreaterThan等对应于每个 C# 比较运算符,因此您不必费心获取运算符方法信息等,只需使用:

var greaterThanCall = Expression.GreaterThanOrEqual(dbFieldMember, criterionConstantMin);
var lessThanCall = Expression.LessThanOrEqual(dbFieldMember, criterionConstantMax);

更新:为了支持 DateTime? 成员,请使用以下内容:

var criterionConstantMin = Expression.Constant(valueDateMin, dbFieldMember.Type);
var criterionConstantMax = Expression.Constant(valueDateMax, dbFieldMember.Type);

然后

var greaterThanCall = Expression.GreaterThanOrEqual(dbFieldMember, criterionConstantMin);
var lessThanCall = Expression.LessThanOrEqual(dbFieldMember, criterionConstantMax);

关于c# - 使用 DateTime 过滤器构建 LINQ to Entities 表达式树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43649673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com