关于LinQ的动态Or查询

Jessie丢 2017-12-12 10:58:17
表SchoolInfo
  public class SchoolInfo
{
public int ID { get; set; }
public string SchoolName { get; set; }
public string RunningSchoolProperty { get; set; }
public string SchoolType { get; set; }
public string SchoolNature { get; set; }
}

ID	SchoolName	RunningSchoolProperty	SchoolType	SchoolNature
1 上海理工大学附属中学 教育部门办 高中 示范性中学
2 中原中学 教育部门办 高中 示范性中学
3 上海财经大学附属中学 教育部门办 高中 示范性中学
4 同济中学 教育部门办 高中 示范性中学
5 同济中学 教育部门办 高中 示范性中学
6 同济中学 教育部门办 高中 示范性中学
7 杨浦高级中学 教育部门办 高中 示范性中学


表SchoolInfoFive
 public class SchoolInfoFive
{
public int ID { get; set; }
public int SchoolID { get; set; }
public int IsLibrary { get; set; }
public int IsAnQuan { get; set; }
public int IsChuangXin { get; set; }
public int IsTheatre { get; set; }
public int IsGym { get; set; }
public int Status { get; set; }
}

ID	SchoolId	IsLibrary	IsAnQuan	IsChuangXin	IsTheatre	IsGym	Status
1 1 0 0 0 0 0 1
2 2 1 1 1 0 0 1
3 3 1 0 0 1 0 1
4 4 0 1 0 1 1 1
5 5 0 0 0 0 1 1
6 6 1 0 1 1 1 1
7 7 0 0 1 1 0 1
8 8 0 1 0 1 0 1
9 9 1 0 1 0 1 1


SchoolInfoFive的 SchoolID= SchoolInfo的ID
想用LINQ 实现SQL语句
  select a.*   FROM [RPE3D].[dbo].[RPE3D_SchoolInfo] a
left join [RPE3D].[dbo].RPE3D_SchoolInfoFive b on a.ID=b.SchoolId
where b.IsLibrary=1 or b.IsAnQuan=1 or b.IsChuangXin=1

查询结果如下:
ID	SchoolName	RunningSchoolProperty	SchoolType	SchoolNature
2 中原中学 教育部门办 高中 示范性中学
3 上海财经大学附属中学 教育部门办 高中 示范性中学
4 同济中学 教育部门办 高中 示范性中学
6 同济中学 教育部门办 高中 示范性中学
7 杨浦高级中学 教育部门办 高中 示范性中学
8 控江中学 教育部门办 高中 示范性中学
9 控江中学(住宿部) 教育部门办 高中 示范性中学


我这里想用LINQ实现我的SQL语句,OR查询使用PredicateExtensions方法,请大神帮忙
 /// <summary>
/// 构造函数使用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混合时写在AND后的OR有效
/// 构造函数使用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混合时写在OR后面的AND有效
/// </summary>
public static class PredicateExtensions
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }

public static Expression<Func<T, bool>> False<T>() { return f => false; }

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
{
var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);
}

public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
{
var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
}
}
...全文
950 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fontain2 2018-01-16
  • 打赏
  • 举报
回复
		var q=db.SchoolInfos.GroupJoin(
				db.SchoolInfoFives.Where(p=>p.IsLibrary==1||p.IsAnQuan==1||p.IsChuangXin==1),
				p1=>p1.ID,p2=>p2.SchoolID,
				(t1,t2)=>new{t1,t2})
				.SelectMany(t=>t.t2.DefaultIfEmpty(),(school,infofive)=>new {school.t1.ID,school.t1.SchoolName});
			foreach(var s in q.AsEnumerable()){
				listBox1.Items.Add(s.ToString());
			}
fontain2 2018-01-16
  • 打赏
  • 举报
回复
试试这个 var q=db.SchoolInfos.GroupJoin( db.SchoolInfoFives.Where(p=>p.IsLibrary==1||p.IsAnQuan==1||p.IsChuangXin==1), p1=>p1.ID,p2=>p2.SchoolID, (t1,t2)=>new{t1,t2}) .SelectMany(t=>t.t2.DefaultIfEmpty(),(school,infofive)=>new {school.t1.ID,school.t1.SchoolName}); foreach(var s in q.AsEnumerable()){ listBox1.Items.Add(s.ToString()); }
正怒月神 2018-01-04
  • 打赏
  • 举报
回复
同1楼,linq中使用||就好了。干嘛要这么复杂呢?
q107770540 2018-01-04
  • 打赏
  • 举报
回复
var query = from a in [RPE3D].[dbo].[RPE3D_SchoolInfo]
join b in [RPE3D].[dbo].RPE3D_SchoolInfoFive.Where(x=>x.IsLibrary==1 || x.IsAnQuan==1 || x.IsChuangXin==1)
on a.ID equals b.SchoolId into lg
from c in lg.DefaultIfEmpty()
select a;

8,493

社区成员

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

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