Linq表达式生成器datetime.year比较
目前我正在使用ExpressionBuilder生成动态查询 我已经为int、date-time和string操作符创建了动态表达式。现在我有一点被卡住了 我想通过表达式比较datetime的月份部分 我们正在传递datetime属性,并希望为该属性的月份创建表达式 代码: 这里property是我要传递到Tsource的属性的名称 p是Tsource类型的参数表达式 我想要这个月所有生日的结果。您可以使用 Expression.PropertyOrField方法。 但不确定它是否能与实体框架查询一起使用 更新 对于实体框架,可以使用SqlFunctions.DatePart方法 您需要一个表达式。调用将其表示为表达式Linq表达式生成器datetime.year比较,linq,entity-framework,expression-trees,dynamic-linq,Linq,Entity Framework,Expression Trees,Dynamic Linq,目前我正在使用ExpressionBuilder生成动态查询 我已经为int、date-time和string操作符创建了动态表达式。现在我有一点被卡住了 我想通过表达式比较datetime的月份部分 我们正在传递datetime属性,并希望为该属性的月份创建表达式 代码: 这里property是我要传递到Tsource的属性的名称 p是Tsource类型的参数表达式 我想要这个月所有生日的结果。您可以使用 Expression.PropertyOrField方法。 但不确定它是否能与实体框架查
我得到了我问题的答案。我所做的就像 代码: 这里第一个属性引用将给出datetime表达式 再次使用这个表达式,我们可以进入其中一步,访问year属性。
这个月我们可以做同样的事情。您的代码现在做什么?@user1522548这是部分代码,但其想法是使用Operataurs enum和for all dayatype创建动态linq查询。我想访问datetime数据类型的year属性。
public static Expression GetDynamicquery<TSource>(
string property, object value, ParameterExpression p)
{
var propertyReference = Expression.Property(p, property);
var constantReference = Expression.Constant(value);
var expression = Expression.Equal(propertyReference, constantReference);
MethodInfo method = null;
return Expression.LessThanOrEqual(propertyReference, constantReference);
}
public static Expression GetDynamicquery<TSource>(
string property, object value, ParameterExpression p)
{
var propertyReference = Expression.Property(p, property);
propertyReference = Expression.Property(propertyReference, "Year");
var constantReference = Expression.Constant(value);
return Expression.Equal(propertyReference, constantReference);
}