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);
}
{
IQueryable < Blog > query = new DongBlogDataContext().Blogs;
if ( ! String.IsNullOrEmpty( strWhere ))
{
query = query.Where (blog => blog.BlogClass == strWhere );
}
return query.OrderByDescending(blog => blog.CreateDateTime);
}
主查询
生成的SQL和第一个完全相同。