Linq实现字符串拼接多条件查询
Linq实现字符串拼接多条件查询
开发过程中,为提升用户体验,经常会使用到多条件查询,本篇博客介绍如何使用Linq实现字符串拼接多条件查询
一般SQL字符串拼接
1 string sql = "select * from StuInfo s inner join Class c on s.CId=c.CId where 1=1";
2 if (sid != )
3 {
4 sql += " and s.SId=" + sid;
5 }
6 else if (sname != null)
7 {
sql += " and s.SName='" + sname + "'";
9 }
10 return sql.ToString();
Linq字符串拼接
1 var stu = from s in _context.StuInfo
join c in _context.Class on s.CId equals c.CId
3 select new
{
s.SId,
s.SName,
s.SSex,
s.SAge,
s.SHobby,
c.CName
};
12 if (sid != )
{
stu = stu.Where(p => p.SId == sid);//使用lambda表达式
}
else if (sname != null)
{
stu = stu.Where(p => p.SName.Contains(sname));
}
20 stu.ToList();//所有的if只有到此处才会执行
End!
Linq实现字符串拼接多条件查询的更多相关文章
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
以前拼接的写法 set @sql=' select * from table where 1=1 ' if (@addDate is not null) set @sql = @sql+' and a ...
- 通过拼接SQL字符串实现多条件查询
一.通过拼接SQL字符串的方法的好处是: 1.方便查询条件的扩展. 2.简化业务逻辑的判断. 二.例子: 1.界面设计 2.点击查询的代码 /// <summary> /// 按条件查询 ...
- Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例
单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...
- MongoDB 字符串值长度条件查询
在实际项目中常常会有根据字段值长度大小进行限制查询,例如查询商品名称过长或过短的商品信息,具体的实现方式可能有多种,在此记录常见的两种实现 使用 $where 查询(性能稍逊一些) 1 2 3 4 5 ...
- 必须知道的SQL编写技巧,多条件查询不拼字符串的写法
在做项目中,我们经常遇到复杂的查询方法,要根据用户的输入,判断某个参数是否合法,合法的话才能当作过滤条件,我们通常的做法是把查询SQL赋值给一个字符串变量,然后根据判断条件动态的拼接where条件进行 ...
- 多条件搜索问题 -sql拼接与参数化查询
来源:传智播客 免费开发视频. 问题:根据书名或出版社或作者查询书籍信息. using System; using System.Collections.Generic问题; using Syste ...
- Rafy 中的 Linq 查询支持(根据聚合子条件查询聚合父)
为了提高开发者的易用性,Rafy 领域实体框架在很早开始就已经支持使用 Linq 语法来查询实体了.但是只支持了一些简单的.常用的条件查询,支持的力度很有限.特别是遇到对聚合对象的查询时,就不能再使用 ...
- linq 多条件查询
Linq 进行多条件查询的时候使用PredicateBuilder帮助类可以很好的解决. 类的源码: public static class PredicateBuilder { /// <su ...
- Webform中linq to sql多条件查询(小练习)
多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: <body> <form id="form1" ...
随机推荐
- 08配置基础路径 同时导出一个函数和一个变量 封装微信请求Api
地址===>https://www.bilibili.com/video/av58993009/?p=46 1==>配置基础路径同时导出一个函数和一个变量 var mynetwork= f ...
- c语言的布尔量
#include <stdio.h> #include <stdbool.h> int main() { bool b = true; bool t = false; ; }
- error: exportArchive: The data couldn’t be read because it isn’t in the correct format.
在执行ios 打包的时候,我们通过执行下面的指令来打包ipa: mkdir arch archive_path=arch/${app_name}.xcarchive workspace_name=HP ...
- http异步通信
get 请求1)创建一个XMLHttpRequest对象2)调用该对象的open方法3)如果是get请求,设置回调函数onreadystatechange = callback4)Send如果是pos ...
- 在centos8搭建dhcp服务
1.用这两个命令就可以安装成功 yum - y install dhcp yum -y install dhcp-server.x86_64 2.配置文件在 /etc/dhcp/dhcpd.conf ...
- How to display `top` results sorted by memory usage in real time?
If you're using the top that comes with Ubuntu (top -v = procps-ng version 3.3.10), then you can use ...
- Boost Graph Library使用学习
Boost Graph Library,BGL 使用学习 探索 Boost Graph Library https://www.ibm.com/developerworks/cn/aix/librar ...
- Linux性能优化实战学习笔记:第四十九讲
一.上节回顾 上一期,我们一起梳理了,网络时不时丢包的分析定位和优化方法.先简单回顾一下.网络丢包,通常会带来严重的性能下降,特别是对 TCP 来说,丢包通常意味着网络拥塞和重传,进而会导致网络延迟增 ...
- [LeetCode] 743. Network Delay Time 网络延迟时间
There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...
- [LeetCode] 658. Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...