背景

通过多个部门id获取所有用户,部门和用户是多对多。

已知部门id,获取该部门包括该部门下的所有子部门的所有用户。

关系如下:

public class Entity:IEntity
{
public Guid Id { get; set; } public string CreateUser { get; set; }
public DateTime CreateTime { get; set; }
public string ModifyUser { get; set; }
public DateTime? ModifyTime { get; set; }
[Timestamp]
public Byte[] RowVersion { get; set; }
}
//组织架构
public class WMS_Org : Entity
{
public WMS_Org() { } public string OrgName { get; set; }
public string OrgDesc { get; set; }
public int OrgOrder { get; set; }
public string OrgFatherId { get; set; }
public virtual ICollection<WMS_OrgUser> OrgUserIds { get; set; }
}
//用户
public class WMS_User : Entity
{
public WMS_User() { } public string UserName { get; set; }
public string NickName { get; set; }
public string UserPwd { get; set; }
public string Sex { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string QQ { get; set; }
public string Address { get; set; }
public string Remark { get; set; }
public virtual ICollection<WMS_OrgUser> UserOrgIds { get; set; }
}
//组织架构和用户的关联
public class WMS_OrgUser : Entity
{
public WMS_OrgUser() { } public Guid OrgId { get; set; }
[ForeignKey("OrgId")]
public virtual WMS_Org Org { get; set; } public Guid UserId { get; set; }
[ForeignKey("UserId")]
public virtual WMS_User User { get; set; }
}
已知组织的id

如上图,我单击0100组织架构,要获取该组织架构下的所有用户,首先由于用户是挂载到每个节点上的,所有需要事先遍历获取所有的子节点获取节点的id

放到Guid[]数组里

提交后台查询
//DBSET=DbSet<WMS_User>,ids=Guid[] ids参数
//第一种方式All
DBSET.Where(p => p.UserOrgIds.All(o=>ids.Contains(o.OrgId))); //第二种方式Any
DBSET.Where(p => p.UserOrgIds.Any(o=>ids.Contains(o.OrgId)));

注意EF采用Contains构造in的sql语句

很直观的可以看出两个区别,一个是All,一个Any,那么产生的sql有什么区别呢?

//All,EF产生的是inner Join
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[UserName] AS [UserName],
[Extent1].[NickName] AS [NickName],
[Extent1].[UserPwd] AS [UserPwd],
[Extent1].[Sex] AS [Sex],
[Extent1].[Phone] AS [Phone],
[Extent1].[Email] AS [Email],
[Extent1].[QQ] AS [QQ],
[Extent1].[Address] AS [Address],
[Extent1].[Remark] AS [Remark],
[Extent1].[Disable] AS [Disable],
[Extent1].[CreateUser] AS [CreateUser],
[Extent1].[CreateTime] AS [CreateTime],
[Extent1].[ModifyUser] AS [ModifyUser],
[Extent1].[ModifyTime] AS [ModifyTime],
[Extent1].[RowVersion] AS [RowVersion]
FROM [dbo].[WMS_User] AS [Extent1]
WHERE NOT EXISTS (
SELECT 1 AS [C1]
FROM [dbo].[WMS_OrgUser] AS [Extent2]
WHERE ([Extent1].[Id] = [Extent2].[UserId])
AND (
(
NOT (
[Extent2].[OrgId] IN (CAST('4e108b91-be3c-4cf4-a074-3767b9b8c7dc' AS UNIQUEIDENTIFIER),
CAST('1cc15f8c-3758-47d3-862a-5287ab5d0651' AS UNIQUEIDENTIFIER),
CAST('e1bf6f5a-800a-4fd4-9309-576c1d84ba2f' AS UNIQUEIDENTIFIER),
CAST('a436b8a9-4b3a-4bf9-bbdd-839571e03915' AS UNIQUEIDENTIFIER),
CAST('b0d6aead-e72f-4fd5-b2cd-e575454d3a4c' AS UNIQUEIDENTIFIER))
)
)
OR (
CASE
WHEN (
[Extent2].[OrgId] IN (
CAST('4e108b91-be3c-4cf4-a074-3767b9b8c7dc' AS UNIQUEIDENTIFIER),
CAST('1cc15f8c-3758-47d3-862a-5287ab5d0651' AS UNIQUEIDENTIFIER),
CAST('e1bf6f5a-800a-4fd4-9309-576c1d84ba2f' AS UNIQUEIDENTIFIER),
CAST('a436b8a9-4b3a-4bf9-bbdd-839571e03915' AS UNIQUEIDENTIFIER),
CAST('b0d6aead-e72f-4fd5-b2cd-e575454d3a4c' AS UNIQUEIDENTIFIER))
) THEN CAST(1 AS BIT)
WHEN (
NOT (
[Extent2].[OrgId] IN (
CAST('4e108b91-be3c-4cf4-a074-3767b9b8c7dc' AS UNIQUEIDENTIFIER),
CAST('1cc15f8c-3758-47d3-862a-5287ab5d0651' AS UNIQUEIDENTIFIER),
CAST('e1bf6f5a-800a-4fd4-9309-576c1d84ba2f' AS UNIQUEIDENTIFIER),
CAST('a436b8a9-4b3a-4bf9-bbdd-839571e03915' AS UNIQUEIDENTIFIER),
CAST('b0d6aead-e72f-4fd5-b2cd-e575454d3a4c' AS UNIQUEIDENTIFIER))
)
) THEN CAST(0 AS BIT)
END IS NULL
)
)
)
//Any方式,EF生成的Exists
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[UserName] AS [UserName],
[Extent1].[NickName] AS [NickName],
[Extent1].[UserPwd] AS [UserPwd],
[Extent1].[Sex] AS [Sex],
[Extent1].[Phone] AS [Phone],
[Extent1].[Email] AS [Email],
[Extent1].[QQ] AS [QQ],
[Extent1].[Address] AS [Address],
[Extent1].[Remark] AS [Remark],
[Extent1].[CreateUser] AS [CreateUser],
[Extent1].[CreateTime] AS [CreateTime],
[Extent1].[ModifyUser] AS [ModifyUser],
[Extent1].[ModifyTime] AS [ModifyTime],
[Extent1].[RowVersion] AS [RowVersion]
FROM [dbo].[WMS_User] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[WMS_OrgUser] AS [Extent2]
WHERE ([Extent1].[Id] = [Extent2].[UserId])
AND ([Extent2].[OrgId] IN
(cast('bd3c010c-476f-45e6-8d83-ad741cc55309' as uniqueidentifier),
cast('4e108b91-be3c-4cf4-a074-3767b9b8c7dc' as uniqueidentifier),
cast('1cc15f8c-3758-47d3-862a-5287ab5d0651' as uniqueidentifier),
cast('e1bf6f5a-800a-4fd4-9309-576c1d84ba2f' as uniqueidentifier),
cast('a436b8a9-4b3a-4bf9-bbdd-839571e03915' as uniqueidentifier),
cast('b0d6aead-e72f-4fd5-b2cd-e575454d3a4c' as uniqueidentifier),
cast('79ddb55a-1587-4928-a312-58da0c091459' as uniqueidentifier),
cast('618de87e-2d97-4c0a-b6c1-acd072c305e2' as uniqueidentifier)))
)

也就是说All是包括条件和不在条件之内的任何值,而Any是严格在条件之内的值,但其实我们想要的是Inner Join的方式,相当于是懒加载方式,那么需要加上Include,也就是

IQueryable<WMS_OrgUser> test5 = _db.wmsOrgUser.Include("User").Where(p => ids.Contains(p.OrgId));
SELECT
1 AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[OrgId] AS [OrgId],
[Extent1].[UserId] AS [UserId],
[Extent1].[CreateUser] AS [CreateUser],
[Extent1].[CreateTime] AS [CreateTime],
[Extent1].[ModifyUser] AS [ModifyUser],
[Extent1].[ModifyTime] AS [ModifyTime],
[Extent1].[RowVersion] AS [RowVersion],
[Extent2].[Id] AS [Id1],
[Extent2].[UserName] AS [UserName],
[Extent2].[NickName] AS [NickName],
[Extent2].[UserPwd] AS [UserPwd],
[Extent2].[Sex] AS [Sex],
[Extent2].[Phone] AS [Phone],
[Extent2].[Email] AS [Email],
[Extent2].[QQ] AS [QQ],
[Extent2].[Address] AS [Address],
[Extent2].[Remark] AS [Remark],
[Extent2].[Disable] AS [Disable],
[Extent2].[CreateUser] AS [CreateUser1],
[Extent2].[CreateTime] AS [CreateTime1],
[Extent2].[ModifyUser] AS [ModifyUser1],
[Extent2].[ModifyTime] AS [ModifyTime1],
[Extent2].[RowVersion] AS [RowVersion1]
FROM [dbo].[WMS_OrgUser] AS [Extent1]
INNER JOIN [dbo].[WMS_User] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[Id]
WHERE [Extent1].[OrgId] IN (
cast('4e108b91-be3c-4cf4-a074-3767b9b8c7dc' as uniqueidentifier),
cast('1cc15f8c-3758-47d3-862a-5287ab5d0651' as uniqueidentifier),
cast('e1bf6f5a-800a-4fd4-9309-576c1d84ba2f' as uniqueidentifier),
cast('a436b8a9-4b3a-4bf9-bbdd-839571e03915' as uniqueidentifier),
cast('b0d6aead-e72f-4fd5-b2cd-e575454d3a4c' as uniqueidentifier)
)

  

条件关联查询,并实现分页
var list = dal.FindByPage(start, pageSize, u => u.OrderByDescending(c => c.CreateTime), u => u.UserOrgIds.All(o => ids.Contains(o.OrgId)), out total);
//防止转json时候嵌套异常
var result = from f in list
select new
{
Id = f.Id,
UserName = f.UserName,
Address = f.Address,
Email = f.Email,
Sex = f.Sex,
CreateUser = f.CreateUser,
CreateTime = f.CreateTime,
NickName = f.NickName,
Phone = f.Phone,
QQ = f.QQ,
Remark = f.Remark,
UserPwd = f.UserPwd
};
return result;
 public virtual IEnumerable<T> FindByPage(int startIndex, int pageSize, Func<IQueryable<T>, IOrderedQueryable<T>> order, Expression<Func<T, bool>> filters, out int Total)
{
try
{
IQueryable<T> rs = order(DBSET.Where(filters));
Total = rs.Count();
if (startIndex < || pageSize < )
{
return null;// rs.ToList();
}
else
{
return rs.Skip(startIndex * pageSize).Take(pageSize).ToList();
}
}
catch (Exception e)
{
Total = ;
log.Error("FindByPage:" + order.ToString() + " " + filters.ToString() + " " + e.Message);
return null;
}
}

EF5.x Code First 一对多关联条件查询,Contains,Any,All的更多相关文章

  1. mybatis一对多根据条件查询的查条数

    一对多写了mapper映射之后 根据条件查条数 可以根据主表的唯一id进行分组 在拿到它的count select count(0) over(aa.id),,id,name,age from tab ...

  2. Mybatis学习——一对多关联表查询

    1.实体类 public class Student { private int id; private String name; } public class Classes { private i ...

  3. ThinkPHP5——模型关联(一对多关联)

    关联定义 一对多关联的情况也比较常见,使用hasMany方法定义,参数包括: hasMany('关联模型名','外键名','主键名',['模型别名定义']); 例如租客表和宿舍表,一个宿舍有多个租客, ...

  4. EF Code First 一对多、多对多关联,如何加载子集合?

    应用场景 先简单描述一下标题的意思:使用 EF Code First 映射配置 Entity 之间的关系,可能是一对多关系,也可能是多对多关系,那如何加载 Entity 下关联的 ICollectio ...

  5. [转]NHibernate之旅(10):探索父子(一对多)关联查询

    本节内容 关联查询引入 一对多关联查询 1.原生SQL关联查询 2.HQL关联查询 3.Criteria API关联查询 结语 关联查询引入 在NHibernate中提供了三种查询方式给我们选择:NH ...

  6. MVC3+EF5.0 code first+Flexigrid+ajax请求+jquery dialog 增删改查

    MVC3+EF5.0 code first+Flexigrid+ajax请求+jquery dialog 增删改查 本文的目的:   1.MVC3项目简单配置EF code first生成并初始化数据 ...

  7. mybatis collection 一对多关联查询,单边分页的问题总结!

    若想直接通过sql实现多级关联查询表结构得有2 个必不可少的字段:id ,parentId,levelId id:主键id, parentId:父id levelId:表示第几级(表本身关联查询的时候 ...

  8. MyBatis关联查询、多条件查询

    MyBatis关联查询.多条件查询 1.一对一查询 任务需求; 根据班级的信息查询出教师的相关信息 1.数据库表的设计 班级表: 教师表: 2.实体类的设计 班级表: public class Cla ...

  9. Django---Django的ORM的一对多操作(外键操作),ORM的多对多操作(关系管理对象),ORM的分组聚合,ORM的F字段查询和Q字段条件查询,Django的事务操作,额外(Django的终端打印SQL语句,脚本调试)

    Django---Django的ORM的一对多操作(外键操作),ORM的多对多操作(关系管理对象),ORM的分组聚合,ORM的F字段查询和Q字段条件查询,Django的事务操作,额外(Django的终 ...

随机推荐

  1. 温故而知新 原来 cheerio 还可以操作XML

  2. 跨系统-跨数据库访问表或创建映射视图-PB访问数据库的特殊处理

    一. 在项目中可能会遇到在一个项目中会出现多个数据库的操作. 二. 也可能会多个系统多个数据库之间进行交互操作显示. 三. 经常需要两个数据库关联查询或需要翻译显示内容时会很麻烦. 四. 今天就有个简 ...

  3. bat 批处理脚本定时执行命令

    有个需求,需要每天定时执行下某个任务,一天一次.由于工作机器环境问题,没有办法设置windows 定时任务.查找资料并完成如下脚本. 功能:每天定时执行一次任务. 复制如下脚本,到一个test.vbs ...

  4. php分享二十五:跨域请求

    问题: 跨域请求有几种方式? jsonp支持哪几种请求方式? 支持post请求吗? 支持get请求吗? 由于浏览器同源策略,凡是发送请求url的协议.域名.端口三者之间任意一与当前页面地址不同即为跨域 ...

  5. GCD 实现生产-消费 模式

    #import "ViewController.h" @interface ViewController (){ } @property (nonatomic,strong) di ...

  6. Atitit 面试问题总结

    Atitit 面试问题总结 1. 面试约人阶段可以预先1俩分钟大概问下情况1 2. 自我介绍阶段1 3. 技术方面2 3.1. 界面方面2 3.2. Java 本身   了解spring mybati ...

  7. 【Unity】7.4 游戏外设输入

    分类:Unity.C#.VS2015 创建日期:2016-04-21 一.简介 Unity可以处理摇杆.游戏手柄.方向盘等标准游戏外设的输入,使用的方法如下图所示: 虚拟按键需要在输入管理器中配置,把 ...

  8. sql左右连接测试

    with a as (select 1 as id, 'name1'as nameunionselect 2 as id, 'name2'as nameunionselect 3 as id, 'na ...

  9. redisHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using ServiceS ...

  10. JVM 类加载机制详解

    如下图所示,JVM类加载机制分为五个部分:加载,验证,准备,解析,初始化,下面我们就分别来看一下这五个过程. 加载 加载是类加载过程中的一个阶段,这个阶段会在内存中生成一个代表这个类的java.lan ...