简体   繁体   English

实体框架 4:将字符串条件转换为 lambda 表达式?

[英]Entity Framework 4: convert a string condition to a lambda expression?

I want to accept a string array of where conditions from the client like field == value .我想接受来自客户端的 where 条件的字符串数组,例如field == value It would be really nice to create a specification object that could accept the string in the constructor and output a lambda expression to represent the Where clause.创建一个可以接受构造函数中的字符串的规范 object 和 output 一个 lambda 表达式来表示 Where 子句会非常好。 For example, I could do the following:例如,我可以执行以下操作:

var myCondition = new Specification<Product>( myStringArrayOfConditions); 
var myProducts = DB.Products.Where( myCondition);

How could you turn "name == Jujyfruits" into你怎么能把"name == Jujyfruits"变成
DB.Products.Where(p => p.name == "JujyFruits") ? DB.Products.Where(p => p.name == "JujyFruits")

You can use您可以使用

  • Reflection to get the Property Product.name from the string name and从字符串name中获取属性Product.name的反射和
  • the LINQ Expression class to manually create a lambda expression. LINQ Expression class 手动创建 lambda 表达式。

Note that the following code example will only work for Equals (==) operations.请注意,以下代码示例仅适用于Equals (==)操作。 However, it is easy to generalize to other operations as well (split on whitespace, parse the operator and choose the appropriate Expression instead of Expression.Equal ).但是,它也很容易推广到其他操作(按空格分割,解析运算符并选择适当的 Expression 而不是Expression.Equal )。

    var condition = "name == Jujyfruits";

    // Parse the condition
    var c = condition.Split(new string[] { "==" }, StringSplitOptions.None);
    var propertyName = c[0].Trim();
    var value = c[1].Trim();

    // Create the lambda
    var arg = Expression.Parameter(typeof(Product), "p");
    var property = typeof(Product).GetProperty(propertyName);
    var comparison = Expression.Equal(
        Expression.MakeMemberAccess(arg, property),
        Expression.Constant(value));
    var lambda = Expression.Lambda<Func<Product, bool>>(comparison, arg).Compile();

    // Test
    var prod1 = new Product() { name = "Test" };
    var prod2 = new Product() { name = "Jujyfruits" };
    Console.WriteLine(lambda(prod1));  // outputs False
    Console.WriteLine(lambda(prod2));  // outputs True

About the constructor thing: Since Func<T, TResult> is sealed, you cannot derive from it.关于构造函数:由于Func<T, TResult>是密封的,因此您不能从中派生。 However, you could create an implicit conversion operator that translates Specification<T> into Func<T, bool> .但是,您可以创建一个隐式转换运算符,将Specification<T>转换为Func<T, bool>

We've recently discovered the Dynamic LINQ library from the VS2008 sample projects.我们最近从 VS2008 示例项目中发现了 Dynamic LINQ 库。 Works perfectly to turn string based "Where" clauses into expressions.完美地将基于字符串的“Where”子句转换为表达式。

This link will get you there. 此链接将带您到达那里。

You need to turn your search term into a predicate.你需要把你的搜索词变成一个谓词。 Try something like the following:尝试以下操作:

string searchString = "JujyFruits";
Func<Product, bool> search = new Func<Product,bool>(p => p.name == searchString);

return DB.Products.Where(search);

暂无
暂无

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

相关问题 实体框架 - 将 SQL 查询转换为实体框架 Lambda 表达式 - Entity Framework - Convert an SQL query to Entity Framework Lambda expression 实体框架asp.net mvc5-无法将lambda表达式转换为类型“字符串”,因为它不是委托类型 - Entity Framework asp.net mvc5- Cannot convert lambda expression to type 'string' because it is not a delegate type 实体框架 - 无法将 lambda 表达式转换为类型“string”,因为它不是委托类型 - Entity Framework - Cannot convert lambda expression to type 'string' because it is not a delegate type 使用实体框架比较字符串和日期时间的 Lambda 表达式 - Lambda expression with comparisons of string and datetime using Entity Framework 实体框架-Lambda表达式中的NotSupportedException - Entity framework - NotSupportedException in lambda expression 如何&#39;不&#39;实体框架的lambda表达式 - how to 'not' a lambda expression for entity framework 将字符串转换为Lambda表达式C#(将linq转换为实体) - Convert string into lambda expression c# (linq to entity) 实体框架查询的 C# Linq Lambda 表达式将自定义表达式传递给 Where 条件 - C# Linq Lambda Expression for Entity Framework Query Passing Custom Expression to Where Condition 如何将具有属性名称的字符串转换为实体框架存储表达式? - How to convert string with property name to Entity framework storage expression? 实体框架 - 使用lambda表达式编写查询 - Entity Framework - writing query using lambda expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM