Linq动态条件处理方法

动态构造Query
同第一种查询更好的写法:

     private  IQueryable < Blog >  getQuery(string strWhere)
    
{
        IQueryable
< Blog >  query  =   new  DongBlogDataContext().Blogs;
        
if  ( ! String.IsNullOrEmpty(
strWhere ))
        
{
           
query  =  query.Where (blog  =>  blog.BlogClass  ==
strWhere );
        }


        
return  query.OrderByDescending(blog  =>  blog.CreateDateTime);
    }

主查询

var result  =  getQuery("400电话 http://www.my400800.cn ");

生成的SQL和第一个完全相同。

原文地址:https://www.cnblogs.com/jishu/p/1940053.html