概述
在多个表联合查询时,使用linq语句查询就显得不那么方便了,执行效率也不高,
SQL语句查询的优势就显现出来了。
using (var context = new YZS_TRAEntities()) { using (Domain.HRModelsContainer hr = new Domain.HRModelsContainer()) {var officeEntities = context.事务所主任.Where(f => officeIds.Contains(f.事务所主任ID)).ToList(); #region 获取该协议所有的对象 var mobileEntities = officeEntities.Select(f => f.手机.Trim()).ToList(); string mobile = string.Join(",", mobileEntities); //List数组的每个元素加上引号,如("12","32","5456","876455") string s1 = string.Format("'{0}'", mobile.Replace(",", "','")); string sql = string.Format(@"SELECT d.Name [DepartName],c.Name [Position],c.No [PostNo],a.Mobile [EmployeePhone] FROM [HYSYZSCCODB].[dbo].[Employee] a //[DepartName] [Position] [PostNo] [EmployeePhone] 是实体officeDTO定义的字段,即转对象 inner join [HYSYZSCCODB].[dbo].[DPEAss] q on a.Id=q.EmployeeId inner join [HYSYZSCCODB].[dbo].[Post] c on q.PostId=c.Id inner join [HYSYZSCCODB].[dbo].[Department] d on d.Id = c.DepartmentId where q.IsHistory='false' and a.Mobile" + " in " + "(" + s1 + ")");
//where a.IsValid='true' and q.IsHistory='false' and {0} order by a.[Index] desc",
//DepartId.HasValue ? "a.Id='" + (Guid)DepartId + "'" + "or " + "a.PDepartmentId='" + (Guid)DepartId + "'" : "1=1");
var entitys = hr.Database.SqlQuery<OfficeDTO>(sql).ToList(); #endregion } }
LIst数组的元素转字符串,(以便用于in查询)
var mobileEntities = officeEntities.Select(f => f.手机.Trim()).ToList(); string mobile = string.Join(",", mobileEntities); //List数组的每个元素加上引号,如("12","32","5456","876455") string s1 = string.Format("'{0}'", mobile.Replace(",", "','"));
转载于:https://www.cnblogs.com/likui-bookHouse/p/9295773.html
最后
以上就是孝顺硬币为你收集整理的SQL语句结合上下文查询(in查询)的全部内容,希望文章能够帮你解决SQL语句结合上下文查询(in查询)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复