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

发布于 2024-11-04 15:47:44 字数 421 浏览 2 评论 0原文

我想接受来自客户端的 where 条件的字符串数组,例如 field == value。 如果创建一个规范对象,它可以接受构造函数中的字符串并输出 lambda 表达式来表示Where 子句,那就太好了。例如,我可以执行以下操作:

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

您如何将 "name == Jujyfruits" 转换为
DB.Products.Where(p => p.name == "JujyFruits “)?

I want to accept a string array of where conditions from the client like 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. 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
DB.Products.Where(p => p.name == "JujyFruits")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我很坚强 2024-11-11 15:47:44

您可以使用

  • Reflection 从字符串 name 中获取属性 Product.name,并使用
  • LINQ Expression 类来手动创建 lambda 表达式。

请注意,以下代码示例仅适用于Equals (==) 运算。然而,它也很容易推广到其他操作(按空格分割、解析运算符并选择适当的表达式而不是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

关于构造函数:由于 Func 是密封的,因此您无法从中派生。但是,您可以创建一个隐式转换运算符来转换Specification< ;T> 转换为 Func

You can use

  • Reflection to get the Property Product.name from the string name and
  • the LINQ Expression class to manually create a lambda expression.

Note that the following code example will only work for Equals (==) operations. 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).

    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. However, you could create an implicit conversion operator that translates Specification<T> into Func<T, bool>.

梦断已成空 2024-11-11 15:47:44

我们最近从 VS2008 示例项目中发现了动态 LINQ 库。非常适合将基于字符串的“Where”子句转换为表达式。

此链接将带您到达那里。

We've recently discovered the Dynamic LINQ library from the VS2008 sample projects. Works perfectly to turn string based "Where" clauses into expressions.

This link will get you there.

居里长安 2024-11-11 15:47:44

您需要将搜索词变成谓词。尝试如下操作:

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

return DB.Products.Where(search);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文