当前位置:网站首页>LINQ implements query string splicing: and and or
LINQ implements query string splicing: and and or
2022-07-18 07:19:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
N Years ago, we came like this Concatenate query strings :
// Why hovertree.com
public string Test(string a, string b, string c,string d)
{
string sql = "SELECT * FROM Users WHERE 1=1";
if (!string.IsNullOrEmpty(a))
{
sql += " AND name='" + a + "'";
}
if (!string.IsNullOrEmpty(b))
{
sql += " AND age='" + b+ "'";
}
if (!string.IsNullOrEmpty(c))
{
sql += " AND sex='" + c + "'";
}
if (!string.IsNullOrEmpty(d))
{
sql += " AND address='" + d + "'";
}
return sql.ToString();
} Now we use linq To implement the code above :
public void Test(string a, string b, string c,string d)
{
QueryContext query = new QueryContext();
var q = from u in query.Users
select u;
if (!string.IsNullOrEmpty(a))
{
q = q.Where(p => p.name == a);
}
if (!string.IsNullOrEmpty(b))
{
q = q.Where(p => p.age == b);
}
if (!string.IsNullOrEmpty(c))
{
q = q.Where(p => p.sex == c);
}
if (!string.IsNullOrEmpty(d))
{
q = q.Where(p => p.address == d);
}
q.ToList(); // All the above if, It will only be executed here
}// Why hovertree.comPublisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/120406.html Link to the original text :https://javaforall.cn
边栏推荐
- What is the distributed query engine of polardb for PostgreSQL?
- C语言:【位域操作】(结构体中使用冒号)
- [basic use of oscilloscope] and [introduction to the meaning of each key on the oscilloscope key panel]
- MySQL触发器
- TP5的whereOr方法多条件存在
- Eight guidelines for modbus-rs485 wiring
- ASP.NET里的Session详细解释
- LeeCode机器人的运动范围
- 如何将面向Oracle开发的应用所使用到的复杂的SQL语句,迁移至基于PolarDB for Pos
- The most important diagram of machine learning, how to select the model sklearn structure diagram
猜你喜欢

Getting started with OpenCV ----- vs how to install opencv Library

左旋转字符串
![[use win10's own remote connection tool] to remotely access and control [another win10 Computer]](/img/a8/c58e43b4e2441db9dc57e1751ee262.png)
[use win10's own remote connection tool] to remotely access and control [another win10 Computer]

Based on taskscheduler and crontask, realize the function of dynamically adding and deleting scheduled tasks

Typora changes text color and line breaks

Intel releases open source AI Reference Suite

MODBUS-RS485布线的8条准则
![[basic use of oscilloscope] and [introduction to the meaning of each key on the oscilloscope key panel]](/img/b7/d2d60ac3c3c1001d49073ea39c2851.png)
[basic use of oscilloscope] and [introduction to the meaning of each key on the oscilloscope key panel]

If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"

TP5的whereOr方法多条件存在
随机推荐
“智慧工厂”上线,破局传统工厂数字化转型
OpenHarmony模块二下文件samgr_server解析(3)
模块二interfaces下头文件解析
PHP表单数据写入MySQL代码
MySQL autoincrement, index, foreign key, other operations
TP5重写分页
自增(自减)运算符的运算优先级
Insert any element at the specified position in the array and delete the element with value x in the array
OpenHarmony模块二初分析
MySQL触发器
DevSecOps研发安全实践——设计篇
mysql中 decimal(10,2)格式的通过stream 方式写到 kafka 变成 stri
Probability meditation: 1 Plausible reasoning
用C的数组实现矩阵的加减乘与求转置
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
Unet++ network tensorflow Version (slim Implementation)
openCV入门----VS怎么安装openCV库
根据verilog代码画电路图
布尔代数值
consul启动闪退