where or or in 我这样复杂的查询方式,又该怎么写 返回
select * from student as s
where (s.name like '%李%' or s.remark like '%李%' or s.cid in (select cid from sclass where name like '%李%'))
and s.bid in (select bid from bsupply where bcount=11)
我这样写的
var _sql=_db.Queryable<SupplyInfo>().With(SqlWith.NoLock)
.WhereIF(!string.IsNullOrWhiteSpace(info.Name), it => it.Name.Contains(info.Name) || it.remark.Contains(info.Name));
_sql=_sql.In(it => it.Cid,_db.Queryable<sclass>().With(SqlWith.NoLock).Where(g => g.Name.Contains(info.Name).Select(g => g.Cid)
.....
怎么能让前面三个or 条件成为一组,我现在的写法 会变成 where ( or ) and in () and in ()
热忱回答(7)
-
Mr、Felix VIP0
2018/10/12这里提问错误
var _sql=_db.Queryable<Student>().With(SqlWith.NoLock)
重点是 第一组where 条件,百思不得其解
0 回复 -
fate stay night VIP0
2018/10/12select cid from sclass where name like '%李%')
分2个查询就好
查询所有 like李的ID 存到 list<string> inIds
然后
var _sql=_db.Queryable<SupplyInfo>().With(SqlWith.NoLock)
.WhereIF(!string.IsNullOrWhiteSpace(info.Name), it => it.Name.Contains(info.Name) || it.remark.Contains(info.Name)|| inids.contains(it=>it.id)).select(it=>it.id);
0 回复 -
Mr、Felix VIP0
2018/10/12@fate stay night:表sclass 的数据量比较庞大,如果存入 list中,可能会导致频繁 从数据库读取大量数据
0 回复 -
fate stay night VIP0
2018/10/12可以用union all实现
db.unionall(queryable1,queryable) 前2个条件可以生成一个query 后面的子查询单独返回一个结果集
0 回复 -
fate stay night VIP0
2018/10/12去重复的应该是db.union()
0 回复 -
Mr、Felix VIP0
2018/10/12@fate stay night:我最后无奈 也是这么做了,可以用
如果不去重,有性能上的影响么0 回复 -
fate stay night VIP0
2018/10/12unionall性能更好些
0 回复