如何建立一个Sql的Linq一点where子句位? [英] How to build up a Linq to Sql where clause bit by bit?

查看:88
本文介绍了如何建立一个Sql的Linq一点where子句位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递的一组查询字符串参数的参数类与查询图像数据库中。随着每次调用一些参数可以由空。因此,在SQL我会建立像

I am being passed a set of querystring parameters within a Parameters class with which to query an image database. With each call some parameters may by null. So in sql I would build up the query like

if (parameters.Value1 != null)
{
    sql.Append("sql_where_clause");
}

if (parameters.Value2 != null)
{
    sql.Append("sql_where_clause");
}



我如何做使用LINQ一样吗?

How do I do the same using Linq?

推荐答案

容易,IQueryables不会被评估,直到你枚举,于是干脆保持标签上的where子句。

easy, IQueryables aren't evaluated until you enumerate, so simply keep tagging on where clauses.

if (parameters.Value1 != null)
{
    results = results.Where(x => <some condition>);
}

if (parameters.Value2 != null)
{
    results = results.Where(x => <some other condition>);
}

这篇关于如何建立一个Sql的Linq一点where子句位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆