使用表达式的 LINQ 过滤器实现

2023-12-26

在 MVC4 中,我向用户提供搜索框来搜索表中的任何值。 所以我在服务器端用 C# 实现通用过滤条件

需要帮助将多个表达式组合成单个表达式

 Expression<Func<T, bool>> 

例如

表格列

MenuText、角色名称(Role.Name 映射)、ActionName

现在,如果用户在搜索框中输入 ABC ,它可以位于显示列中的任何行中,则需要进行过滤。

Model

public class Menu
{
  public string MenuText {get;set;}
  public Role Role {get;set;}
  public string ActionName {get;set;}
}

public class Role
{
  public string Name {get;set;}
}

到目前为止我已经实施了

  /// <summary>
    /// string[] properties property.Name (MenuText, ActionName), including deeper Mapping names such as (Role.Name)
    /// </summary>
    public static Expression<Func<T, bool>> FilterKey<T>(string filterText, params string[] properties)
    {
        ParameterExpression parameter = Expression.Parameter(typeof (T));
        Expression[] propertyExpressions = properties.Select(
            x => !string.IsNullOrEmpty(x) ? GetDeepPropertyExpression(parameter, x) : null).ToArray();

        Expression<Func<T, bool>> predicate = PredicateBuilder.False<T>();
        foreach (Expression expression in propertyExpressions)
        {
             var toLower = Expression.Call(expression, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
            var like = Expression.Call(toLower, typeof(string).GetMethod("Contains"), Expression.Constant(filterText.ToLower()));
            //TODO: Combine expressions to form single  Expression<Func<T, bool>> expression

        }
        return predicate;
    }

       /// <summary>
        ///  To Get Deeper Properties such as Role.Name Expressions
        /// </summary>
       private static Expression GetDeepPropertyExpression(Expression initialInstance, string property)
        {
            Expression result = null;
            foreach (string propertyName in property.Split('.'))
            {
                Expression instance = result ?? initialInstance;
                result = Expression.Property(instance, propertyName);
            }
            return result;
        }

我创建了一些搜索IQueryable您应该能够使用的扩展方法

完整的博客文章在这里:

http://jnye.co/Posts/6/c%23-generic-search-extension-method-for-iqueryable http://jnye.co/Posts/6/c%23-generic-search-extension-method-for-iqueryable

GitHub 项目在这里(有一些额外的扩展OR搜索:

https://github.com/ninjanye/SearchExtensions https://github.com/ninjanye/SearchExtensions

public static class QueryableExtensions  
{  
    public static IQueryable<T> Search<T>(this IQueryable<T> source, Expression<Func<T, string>> stringProperty, string searchTerm)  
    {  
        if (String.IsNullOrEmpty(searchTerm))  
        {  
            return source;  
        }  

        // The below represents the following lamda:  
        // source.Where(x => x.[property] != null  
        //                && x.[property].Contains(searchTerm))  

        //Create expression to represent x.[property] != null  
        var isNotNullExpression = Expression.NotEqual(stringProperty.Body, Expression.Constant(null));  

        //Create expression to represent x.[property].Contains(searchTerm)  
        var searchTermExpression = Expression.Constant(searchTerm);  
        var checkContainsExpression = Expression.Call(stringProperty.Body, typeof(string).GetMethod("Contains"), searchTermExpression);  

        //Join not null and contains expressions  
        var notNullAndContainsExpression = Expression.AndAlso(isNotNullExpression, checkContainsExpression);  

        var methodCallExpression = Expression.Call(typeof(Queryable),  
                                                   "Where",  
                                                   new Type[] { source.ElementType },  
                                                   source.Expression,  
                                                   Expression.Lambda<Func<T, bool>>(notNullAndContainsExpression, stringProperty.Parameters));  

        return source.Provider.CreateQuery<T>(methodCallExpression);  
    }  
}  

这允许您编写如下内容:

string searchTerm = "test";  
var results = context.Menu.Search(menu => menu.MenuText, searchTerm).ToList();  

//OR for Role name
string searchTerm = "test";
var results = context.Menu.Search(menu => menu.Role.Name, searchTerm).ToList();

您可能还会发现以下帖子很有用:

允许搜索多个属性的搜索扩展方法:

http://jnye.co/Posts/7/generic-iqueryable-or-search-on-multiple-properties-using-expression-trees http://jnye.co/Posts/7/generic-iqueryable-or-search-on-multiple-properties-using-expression-trees

允许在属性上使用多个或搜索词的搜索扩展方法:

http://jnye.co/Posts/8/generic-iqueryable-or-search-for-multiple-search-terms-using-expression-trees http://jnye.co/Posts/8/generic-iqueryable-or-search-for-multiple-search-terms-using-expression-trees

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用表达式的 LINQ 过滤器实现 的相关文章

  • WPF DataGrid 多选

    我读过几篇关于这个主题的文章 但很多都是来自 VS 或框架的早期版本 我想做的是从 dataGrid 中选择多行并将这些行返回到绑定的可观察集合中 我尝试创建一个属性 类型 并将其添加到可观察集合中 它适用于单个记录 但代码永远不会触发多个
  • 结构化绑定中缺少类型信息

    我刚刚了解了 C 中的结构化绑定 但有一件事我不喜欢 auto x y some func is that auto正在隐藏类型x and y 我得抬头看看some func的声明来了解类型x and y 或者 我可以写 T1 x T2 y
  • 在 xaml 中编写嵌套类型时出现设计时错误

    我创建了一个用户控件 它接受枚举类型并将该枚举的值分配给该用户控件中的 ComboBox 控件 很简单 我在数据模板中使用此用户控件 当出现嵌套类型时 问题就来了 我使用这个符号来指定 EnumType x Type myNamespace
  • C# 异步等待澄清?

    我读了here http blog stephencleary com 2012 02 async and await html that 等待检查等待的看看它是否有already完全的 如果 可等待已经完成 那么该方法将继续 运行 同步
  • 如何在 Cassandra 中存储无符号整数?

    我通过 Datastax 驱动程序在 Cassandra 中存储一些数据 并且需要存储无符号 16 位和 32 位整数 对于无符号 16 位整数 我可以轻松地将它们存储为有符号 32 位整数 并根据需要进行转换 然而 对于无符号 64 位整
  • std::vector 与 std::stack

    有什么区别std vector and std stack 显然 向量可以删除集合中的项目 尽管比列表慢得多 而堆栈被构建为仅后进先出的集合 然而 堆栈对于最终物品操作是否更快 它是链表还是动态重新分配的数组 我找不到关于堆栈的太多信息 但
  • free 和 malloc 在 C 中如何工作?

    我试图弄清楚如果我尝试 从中间 释放指针会发生什么 例如 看下面的代码 char ptr char malloc 10 sizeof char for char i 0 i lt 10 i ptr i i 10 ptr ptr ptr pt
  • 如何在 C++ 中标记字符串?

    Java有一个方便的分割方法 String str The quick brown fox String results str split 在 C 中是否有一种简单的方法可以做到这一点 The 增强分词器 http www boost o
  • 对类 static constexpr 结构的未定义引用,g++ 与 clang

    这是我的代码 a cp p struct int2 int x y struct Foo static constexpr int bar1 1 static constexpr int2 bar2 1 2 int foo1 return
  • 需要帮助优化算法 - 两百万以下所有素数的总和

    我正在尝试做一个欧拉计划 http projecteuler net问题 我正在寻找 2 000 000 以下所有素数的总和 这就是我所拥有的 int main int argc char argv unsigned long int su
  • 方程“a + bx = c + dy”的积分解

    在等式中a bx c dy 所有变量都是整数 a b c and d是已知的 我如何找到整体解决方案x and y 如果我的想法是正确的 将会有无限多个解 由最小公倍数分隔b and d 但我只需要一个解决方案 我可以计算其余的 这是一个例
  • 结构体的内存大小不同?

    为什么第一种情况不是12 测试环境 最新版本的 gcc 和 clang 64 位 Linux struct desc int parts int nr sizeof desc Output 16 struct desc int parts
  • 为什么这个字符串用AesCryptoServiceProvider第二次解密时不相等?

    我在 C VS2012 NET 4 5 中的文本加密和解密方面遇到问题 具体来说 当我加密并随后解密字符串时 输出与输入不同 然而 奇怪的是 如果我复制加密的输出并将其硬编码为字符串文字 解密就会起作用 以下代码示例说明了该问题 我究竟做错
  • x:将 ViewModel 方法绑定到 DataTemplate 内的事件

    我基本上问同样的问题这个人 https stackoverflow com questions 10752448 binding to viewmodels property from a template 但在较新的背景下x Bind V
  • 实例化类时重写虚拟方法

    我有一个带有一些虚函数的类 让我们假设这是其中之一 public class AClassWhatever protected virtual string DoAThingToAString string inputString retu
  • 复制目录下所有文件

    如何将一个目录中的所有内容复制到另一个目录而不循环遍历每个文件 你不能 两者都不Directory http msdn microsoft com en us library system io directory aspx nor Dir
  • 如何在 Linq to SQL 中使用distinct 和 group by

    我正在尝试将以下 sql 转换为 Linq 2 SQL select groupId count distinct userId from processroundissueinstance group by groupId 这是我的代码
  • 如何在 Android 中使用 C# 生成的 RSA 公钥?

    我想在无法假定 HTTPS 可用的情况下确保 Android 应用程序和 C ASP NET 服务器之间的消息隐私 我想使用 RSA 来加密 Android 设备首次联系服务器时传输的对称密钥 RSA密钥对已在服务器上生成 私钥保存在服务器
  • 使用特定参数从 SQL 数据库填充组合框

    我在使用参数从 sql server 获取特定值时遇到问题 任何人都可以解释一下为什么它在 winfom 上工作但在 wpf 上不起作用以及我如何修复它 我的代码 private void UpdateItems COMBOBOX1 Ite
  • MySQL Connector C/C API - 使用特殊字符进行查询

    我是一个 C 程序 我有一个接受域名参数的函数 void db domains query char name 使用 mysql query 我测试数据库中是否存在域名 如果不是这种情况 我插入新域名 char query 400 spri

随机推荐