关于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);
}
}
...全文
939 4 打赏 收藏 转发到动态 举报
写回复
用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;
关于LINQ的一本书籍,附上源码,喜欢请购买正版。 LINQ is the part of the .NET Framework that provides a generic approach to querying data from different data sources. It has quickly become the next must-have skill for .NET developers. Pro LINQ: Language Integrated Query in C# 2010 is all about code. Literally, this book starts with code and ends with code. Most books show the simplest examples of how to use a method, but they so rarely show how to use the more complex prototypes. This book is different. Demonstrating the overwhelming majority of LINQ operators and prototypes, it is a veritable treasury of LINQ examples. Rather than obscure the relevant LINQ principles in code examples by focusing on a demonstration application you have no interest in writing, this book cuts right to the chase of each LINQ operator, method, or class. However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it. For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts so you can step through the code and see them unfold. Face it, most technical books, while informative, are dull. LINQ need not be dull. Written with a sense of humor, this book will attempt to entertain you on your journey through the wonderland of LINQ and C# 2010. What you’ll learn How to leverage all the new LINQ relevant C# 2008 language features including extension methods, lambda expressions, anonymous data types, and partial methods. How to use LINQ to Objects to query in-memory data collections such as arrays, ArrayLists, and Lists to retrieve the data you want. Why some queries are deferred, how a deferred query can bite you, and how you can make deferred queries work for you. How to use LINQ to XML to revolutionize your creation, manipulation, and searching of XML data. How to query DataSets with LINQ to DataSet so you can coexist with legacy code and use LINQ to query databases other than SQL Server. How to query Databases with LINQ to SQL, write your own entity classes, and understand how to handle concurrency conflicts. Who this book is for This book is written for the proficient C# developer, but you do not need to be up on all the latest C# features to understand the material. When you finish this book, you will have a much greater understanding of the latest C# features. Table of Contents Hello LINQ C# Language Enhancements for LINQ LINQ to Objects Introduction Deferred Operators Nondeferred Operators LINQ to XML Introduction The LINQ to XML API LINQ to XML Operators Additional XML Capabilities LINQ to DataSet Operators Additional DataSet Capabilities LINQ to SQL Introduct ion LINQ to SQL Tips and Tools LINQ to SQL Database Operations LINQ to SQL Ent ity Classes The LINQ to SQL DataContext LINQ to SQL Concurrency Conflicts Additional LINQ to SQL Capabilities LINQ to Entities Introduction LINQ to Entities Operations LINQ to Entities Classes Parallel LINQ Introduction Using Parallel LINQ Parallel LINQ Operators

8,497

社区成员

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

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