Linq多条件随机组合查询

qq_37347502 2018-07-19 02:51:24
数据库一张表里有两个字段,一个是类型 type(出售或者购买),一个是状态 state(进行中,已完成,取消),前端有5个复选框,用户可以随便勾选类型或者状态来进行查询,比如勾选了 出售 购买 进行中 已完成,该怎么写代码
...全文
577 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_25467253 2018-11-20
  • 打赏
  • 举报
回复
求有大神帮忙https://ask.csdn.net/questions/706517
arsalois 2018-11-15
  • 打赏
  • 举报
回复
多看文档。。
  • 打赏
  • 举报
回复
Expression<Func<CSTK.Core.Task.Task, bool>> exp = o => true;
if (type==0) exp = e => e.CreateTime >= begintime && e.CreateTime <= endtime;
........................
threenewbee 2018-07-29
  • 打赏
  • 举报
回复
引用 2 楼 q107770540 的回复:
  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(); //上边的所有if,只有到此处才会执行



https://blog.csdn.net/q107770540/article/details/5724013

就这个办法就可以。
补充下
q = q.where(...) 是 and 的关系。
如果是表达or的关系,可以
q = q.union(data.where(...))
圣殿骑士18 2018-07-28
  • 打赏
  • 举报
回复
你的问题,实际上是一个字段的多条件查询,用 In

List<string> states = new List<string>(){ "1", "2"}; //勾选中的值
var q = query.Where(c=>states.Contains(c.state));
小case 2018-07-28
  • 打赏
  • 举报
回复
比如查询姓名和年龄,textbox1和textbox2来填写(当然你换其他控件也可以啊)
linq 。。。。

where textbox1.text.trim()==""?true: 姓名==“textbox1.text && textbox2.text.trim()==""?true: 年龄==textbox2.text

你要用&& ,||来连接where语句,可以好多个
小case 2018-07-28
  • 打赏
  • 举报
回复
where (textbox1.text.trim()==""?true: 姓名==textbox1.text) && (textbox2.text.trim()==""?true: 年龄==textbox2.text)
q107770540 2018-07-24
  • 打赏
  • 举报
回复
  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(); //上边的所有if,只有到此处才会执行



https://blog.csdn.net/q107770540/article/details/5724013
游北亮 2018-07-19
  • 打赏
  • 举报
回复
linq的基础,随便找一篇文章都能看到吧?类似
var q1 = from item in result where item.Id == 1 || item.Id == 2 select item;

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧