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和第一个完全相同。

posted @ 2010-08-31 09:07  ljlxyf  阅读(192)  评论(0编辑  收藏  举报