ef、linq怎么实现以下的功能
str sqlstr="select a.* from a"
if (code1!="")
sqlstr=sqlstr+" join b on a.id=b.id && b.code=code1"
if (code2!="")
sqlstr=sqlstr+" join c on a.id=c.id && c.code=code1"
问题补充:
在SQL Server Management Studio里直接运行
select distinct(s.Title) from Table1 as si
join Table2 as t on SUBSTRING(t.Code,1,12)='010101010108' and t.tid=si.tid
join ST as st on SUBSTRING(st.Code,1,4)='0104' and st.stid=si.stid
join S as s on SUBSTRING(s.SpellTitle,1,1)='C' and si.sid=s.sid
where si.IsAudited='true' and si.IsDeleted='false' and si.IsVisable='true'
第一次150ms,接下来都是50ms以下。
但是在代码里
IEnumerable<SI> list = _dataContext.SI.Where(a =>
(filterModel == 1 ? a.IsAudited == true && a.IsDeleted == false && a.IsVisable == true : a.IsDeleted == false) &&
(Code == "" ? true : a.ST.Code.StartsWith(Code)) &&
(tid== 0 ? true : a.tid== tid)).Distinct(....);
最少都在500ms以上,更甚的是,用老外的PredicateBuilder构建表达式,最少都在1.5m左右,这样明显有哪里做得不好.
慕运维8079593