如何仅通过表达式创建 where 子句

How to create where clause only by Expression

我想要实现的是一个带有表达式树的简单 where 子句(仅):

Tags.Where(t => t.Title == "Exam")

到目前为止我做了什么:

ParameterExpression pe = Expression.Parameter(typeof(EfTag), "t");
Expression left = Expression.Property(pe, typeof(EfTag).GetProperty("Title"));
Expression right = Expression.Constant("Exam");
Expression e1 = Expression.Equal(left, right);

var neededTags = myDataContext.Tags.where( e1 /* what should i do here WI?? */ );

请不要让我参考System.Linq.Dynamic or LinqKit。我需要在不使用任何第三方库的情况下执行此操作。

你快到了。现在您有了参数和 == 运算符调用,您必须将它们组合在一起成为 Lambda:

var predicate = Expression.Lambda<Func<EfTag, bool>>(e1, pe);

您稍后可以在 Where 调用中使用它:

var neededTags = myDataContext.Tags.Where(predicate);