linq动态条件查询如何使用

发布时间:2021-12-02 09:30:19 作者:iii
来源:亿速云 阅读:135

本篇内容主要讲解“linq动态条件查询如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“linq动态条件查询如何使用”吧!

1,linq动态条件之构造表达式树

  1. private Expression<Func<Blog, bool>> getCondition()  

  2.     {  

  3.         Expression<Func<Blog, bool>> expression = blog => true;  

  4.         if (!String.IsNullOrEmpty(Request["BlogClassID"]))  

  5.         {  

  6.             int blogClassID;  

  7.             if (Int32.TryParse(Request["BlogClassID"], out blogClassID))  

  8.             {  

  9.                 Expression<Func<Blog, bool>> e2 = blog => 

  10. blog.BlogClass == null;  

  11.                 var invokedExpr = Expression.Invoke

  12. (e, expression.Parameters.Cast

    ());  
  13.                 expression = Expression.Lambda<Func<Blog, bool>>
    (Expression.And(expression.Body, invokedExpr), expression.Parameters);  

  14.             }  

  15.         }  

  16.         return expression;  

  17.     } 

主查询是这个样子:

var result = new DongBlogDataContext().Blogs.Where(getCondition());

因为根据SQL追踪,生成SQL类似:

SELECT [t0].[BlogID], [t0].[ChannelID], [t0].[BlogClassID], [t0].[Title], [t0].[Content], [t0].[Tag], [t0].[CreateDateTime]  FROM [dbo].[Blog] AS [t0]  WHERE [t0].[BlogClassID] IS NULL

这种方法是实质是合并Lamba表达式,也就是这三句。

SELECT [t0].[BlogID], [t0].[ChannelID], [t0].[BlogClassID], [t0].[Title], [t0].[Content], [t0].[Tag], [t0].[CreateDateTime]  FROM [dbo].[Blog] AS [t0]  WHERE [t0].[BlogClassID] IS NULL

如果每个条件合并都这么写会很麻烦,幸好已经有人给写好的辅助类:

using System;  using System.Linq;  using System.Linq.Expressions;  using System.Collections.Generic;   public static class PredicateBuilder  {    public static Expression<Func<T, bool>> True ()  { return f => true;  }    public static Expression<Func<T, bool>> False () { return f => false; }     public static Expression<Func<T, bool>> Or (this Expression<Func<T, bool>> expr1,                                                Expression<Func<T, bool>> expr2)    {      var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast ());      return Expression.Lambda<Func<T, bool>>            (Expression.Or (expr1.Body, invokedExpr), expr1.Parameters);    }     public static Expression<Func<T, bool>> And (this Expression<Func<T, bool>> expr1,                                                         Expression<Func<T, bool>> expr2)    {      var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast ());      return Expression.Lambda<Func<T, bool>>            (Expression.And (expr1.Body, invokedExpr), expr1.Parameters);    }  }

这个类可以用于Expression<Func>类型的表达式的合并了。

2,linq动态条件之构造Query

同***种查询更好的写法:

private IQueryable getQuery()      {          IQueryable query = new DongBlogDataContext().Blogs;          if (!String.IsNullOrEmpty(Request["BlogClassID"]))          {              int blogClassID;              if (Int32.TryParse(Request["BlogClassID"], out blogClassID))                  query = query.Where(blog => blog.BlogClass == null);          }          return query.OrderByDescending(blog => blog.CreateDateTime);      }

主查询

var result = getQuery();

生成的SQL和***个完全相同。

到此,相信大家对“linq动态条件查询如何使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 动态linq查询的实现方式是什么
  2. LINQ+Ajax动态查询怎么使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linq

上一篇:GEO数据库架构的原理是什么

下一篇:SpringBoot2.0整合tk.mybatis异常怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》