zoukankan      html  css  js  c++  java
  • Linq多条件关联查询

    在使用Linq今天多表关联查询时关联表查询还算是比较复杂的,今天遇到一个关联表查询而且还是多条件不定项查询,查询条件可有可无,要进行linq查询,一开始也是没思路,网上百度了好多,终于找到解决方法,在where中使用三则运算来判断查询条件是否存在。

                entities =  from e in entities join h in houses on e.HouseId equals h.Id
                             join v in villages on h.VillageId equals v.Id
                             where (search.CompanyId == 0 ? true : v.CompanyId == search.CompanyId)
                             && (string.IsNullOrWhiteSpace(search.KeyWord.Trim()) ? true : (v.Name.Contains(search.KeyWord) || v.CounName.Contains(search.KeyWord) || v.CityName.Contains(search.KeyWord))) 
                             && (string.IsNullOrWhiteSpace(search.CityCode) ? true : v.CityCode == search.CityCode) 
                             && (string.IsNullOrWhiteSpace(search.CounCode) ? true : v.CounCode == search.CounCode) 
                             && (search.CompanyId == 0 ? true : h.CompanyId == search.CompanyId) 
                             && (search.RoomCount == 0 ? true : h.RoomCount == search.RoomCount) 
                             && (search.CompanyId == 0 ? true : e.CompanyId == search.CompanyId) 
                             && (search.RentMaxMoney.HasValue && search.RentMaxMoney.Value > 0 ? e.RentMoney <= search.RentMaxMoney.Value : true) 
                             && (search.RentMinMoney.HasValue && search.RentMinMoney.Value > 0 ? e.RentMoney >= search.RentMinMoney.Value : true) 
                             && (search.RentType.HasValue ? e.RentType == search.RentType.Value:true) 
                             && e.IsOnShelf
                             select e;
    ---------------我是有底线的--------------------
    作者:社会主义接班人
    出处:http://www.cnblogs.com/5ishare/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    大数据架构师技能图谱
    2018年,Java程序员转型大数据开发,是不是一个好选择?
    如何将java web项目上线/部署到公网
    Flume调优
    Spark流处理调优步骤
    zookeeper的WEB客户端zkui使用
    HBase各版本对Hadoop版本的支持情况
    java 代码实现使用Druid 链接池获取数据库链接
    安装postgreSQL出现configure: error: zlib library not found解决方法
    修改postgres密码
  • 原文地址:https://www.cnblogs.com/5ishare/p/14328236.html
Copyright © 2011-2022 走看看