8,497
社区成员
发帖
与我相关
我的任务
分享
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 杨浦高级中学 教育部门办 高中 示范性中学
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
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 控江中学(住宿部) 教育部门办 高中 示范性中学
/// <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);
}
}
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());
}
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;