AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理、职称管理、部门管理、角色管理、角色权限管理等模块。

属于某个角色的用户列表(Any的用法)

使用Subsonic,我们有两种方法获取属于某个角色的用户列表,分别是表关联和子查询。

Subsonic的表关联实现:

// 查询 X_User 表
SqlQuery q = new Select().From<XUser>().InnerJoin(XRoleUser.UserIdColumn, XUser.IdColumn);
q.Where("1").IsEqualTo("1"); // 在用户名称中搜索
string searchText = ttbSearchUser.Text.Trim();
if (!String.IsNullOrEmpty(searchText))
{
q.And(XUser.NameColumn).ContainsString(searchText);
} // 过滤选中角色下的所有用户
object[] values = Grid1.DataKeys[Grid1.SelectedRowIndexArray[0]];
int roleId = Convert.ToInt32(values[0]);
q.And(XRoleUser.RoleIdColumn).IsEqualTo(roleId); // 在查询添加之后,排序和分页之前获取总记录数
// Grid1总共有多少条记录
Grid2.RecordCount = q.GetRecordCount(); // 排列
q.OrderBys.Add(GetSortExpression(Grid2, XUser.Schema)); // 数据库分页
q.Paged(Grid2.PageIndex + 1, Grid2.PageSize);
XUserCollection items = q.ExecuteAsCollection<XUserCollection>(); Grid2.DataSource = items;
Grid2.DataBind();

Subsonic的子查询实现:

// 查询 X_User 表
SqlQuery q = new Select().From<XUser>();
q.Where("1").IsEqualTo("1"); // 在用户名称中搜索
string searchText = ttbSearchUser.Text.Trim();
if (!String.IsNullOrEmpty(searchText))
{
q.And(XUser.NameColumn).ContainsString(searchText);
} // 过滤选中角色下的所有用户
object[] values = Grid1.DataKeys[Grid1.SelectedRowIndexArray[0]];
int roleId = Convert.ToInt32(values[0]);
SqlQuery subQ = new Select(XRoleUser.UserIdColumn).From<XRoleUser>().Where(XRoleUser.RoleIdColumn).IsEqualTo(roleId); q.And(XUser.IdColumn).In(subQ); // 在查询添加之后,排序和分页之前获取总记录数
// Grid1总共有多少条记录
Grid2.RecordCount = q.GetRecordCount(); // 排列
q.OrderBys.Add(GetSortExpression(Grid2, XUser.Schema)); // 数据库分页
q.Paged(Grid2.PageIndex + 1, Grid2.PageSize);
XUserCollection items = q.ExecuteAsCollection<XUserCollection>(); Grid2.DataSource = items;
Grid2.DataBind();

  

使用Entity Framework就不能从数据库的角度思考问题,而应该从实体类之间的关系考虑问题,具体的实现:

IQueryable<User> q = DB.Users;

// 在用户名称中搜索
string searchText = ttbSearchUser.Text.Trim();
if (!String.IsNullOrEmpty(searchText))
{
q = q.Where(u => u.Name.Contains(searchText));
} // 过滤选中角色下的所有用户
object[] values = Grid1.DataKeys[Grid1.SelectedRowIndexArray[0]];
int roleId = Convert.ToInt32(values[0]);
q = q.Where(u => u.Roles.Any(r => r.ID == roleId)); // 在查询添加之后,排序和分页之前获取总记录数
Grid2.RecordCount = q.Count(); // 排列和分页
q = SortAndPage<User>(q, Grid2); Grid2.DataSource = q;
Grid2.DataBind();

  

这里用到了 Any 方法,可以这么理解:检索一些用户,只要用户的任意一个角色是roleId就可以。

来看下生成的SQL语句:

exec sp_executesql N'SELECT TOP (20)
[Project2].[ID] AS [ID],
[Project2].[Name] AS [Name],
[Project2].[Email] AS [Email],
[Project2].[Password] AS [Password],
[Project2].[Enabled] AS [Enabled],
[Project2].[Gender] AS [Gender],
[Project2].[ChineseName] AS [ChineseName],
[Project2].[EnglishName] AS [EnglishName],
[Project2].[Photo] AS [Photo],
[Project2].[QQ] AS [QQ],
[Project2].[CompanyEmail] AS [CompanyEmail],
[Project2].[OfficePhone] AS [OfficePhone],
[Project2].[OfficePhoneExt] AS [OfficePhoneExt],
[Project2].[HomePhone] AS [HomePhone],
[Project2].[CellPhone] AS [CellPhone],
[Project2].[Address] AS [Address],
[Project2].[Remark] AS [Remark],
[Project2].[IdentityCard] AS [IdentityCard],
[Project2].[Birthday] AS [Birthday],
[Project2].[TakeOfficeTime] AS [TakeOfficeTime],
[Project2].[LastLoginTime] AS [LastLoginTime],
[Project2].[CreateTime] AS [CreateTime],
[Project2].[DeptID] AS [DeptID]
FROM ( SELECT [Project2].[ID] AS [ID], [Project2].[Name] AS [Name], [Project2].[Email] AS [Email], [Project2].[Password] AS [Password], [Project2].[Enabled] AS [Enabled], [Project2].[Gender] AS [Gender], [Project2].[ChineseName] AS [ChineseName], [Project2].[EnglishName] AS [EnglishName], [Project2].[Photo] AS [Photo], [Project2].[QQ] AS [QQ], [Project2].[CompanyEmail] AS [CompanyEmail], [Project2].[OfficePhone] AS [OfficePhone], [Project2].[OfficePhoneExt] AS [OfficePhoneExt], [Project2].[HomePhone] AS [HomePhone], [Project2].[CellPhone] AS [CellPhone], [Project2].[Address] AS [Address], [Project2].[Remark] AS [Remark], [Project2].[IdentityCard] AS [IdentityCard], [Project2].[Birthday] AS [Birthday], [Project2].[TakeOfficeTime] AS [TakeOfficeTime], [Project2].[LastLoginTime] AS [LastLoginTime], [Project2].[CreateTime] AS [CreateTime], [Project2].[DeptID] AS [DeptID], row_number() OVER (ORDER BY [Project2].[Name] DESC) AS [row_number]
FROM ( SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Email] AS [Email],
[Extent1].[Password] AS [Password],
[Extent1].[Enabled] AS [Enabled],
[Extent1].[Gender] AS [Gender],
[Extent1].[ChineseName] AS [ChineseName],
[Extent1].[EnglishName] AS [EnglishName],
[Extent1].[Photo] AS [Photo],
[Extent1].[QQ] AS [QQ],
[Extent1].[CompanyEmail] AS [CompanyEmail],
[Extent1].[OfficePhone] AS [OfficePhone],
[Extent1].[OfficePhoneExt] AS [OfficePhoneExt],
[Extent1].[HomePhone] AS [HomePhone],
[Extent1].[CellPhone] AS [CellPhone],
[Extent1].[Address] AS [Address],
[Extent1].[Remark] AS [Remark],
[Extent1].[IdentityCard] AS [IdentityCard],
[Extent1].[Birthday] AS [Birthday],
[Extent1].[TakeOfficeTime] AS [TakeOfficeTime],
[Extent1].[LastLoginTime] AS [LastLoginTime],
[Extent1].[CreateTime] AS [CreateTime],
[Extent1].[DeptID] AS [DeptID]
FROM [dbo].[Users] AS [Extent1]
WHERE (N''admin'' <> [Extent1].[Name]) AND ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[RoleUsers] AS [Extent2]
WHERE ([Extent1].[ID] = [Extent2].[UserID]) AND ([Extent2].[RoleID] = @p__linq__0)
))
) AS [Project2]
) AS [Project2]
WHERE [Project2].[row_number] > 0
ORDER BY [Project2].[Name] DESC',N'@p__linq__0 int',@p__linq__0=1
go

  

EF生成的SQL语句是很复杂,我们来稍微简化一下(为了看清本质,去掉了排序,过滤以及返回字段的个数等):

exec sp_executesql N'SELECT [Project2].[ID] AS [ID], [Project2].[Name] AS [Name], [Project2].[Email] AS [Email], [Project2].[Password] AS [Password]
FROM ( SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Email] AS [Email],
[Extent1].[Password] AS [Password]
FROM [dbo].[Users] AS [Extent1]
WHERE (N''admin'' <> [Extent1].[Name]) AND ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[RoleUsers] AS [Extent2]
WHERE ([Extent1].[ID] = [Extent2].[UserID]) AND ([Extent2].[RoleID] = @p__linq__0)
))
) AS [Project2]',N'@p__linq__0 int',@p__linq__0=1
go

进一步简化:

exec sp_executesql N' SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Email] AS [Email],
[Extent1].[Password] AS [Password]
FROM [dbo].[Users] AS [Extent1]
WHERE (N''admin'' <> [Extent1].[Name]) AND ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[RoleUsers] AS [Extent2]
WHERE ([Extent1].[ID] = [Extent2].[UserID]) AND ([Extent2].[RoleID] = @p__linq__0)
))
',N'@p__linq__0 int',@p__linq__0=1
go

进一步简化:

SELECT
[Users].[ID] AS [ID],
[Users].[Name] AS [Name],
[Users].[Email] AS [Email],
[Users].[Password] AS [Password]
FROM [dbo].[Users] AS [Users]
WHERE EXISTS (SELECT
1 AS [C1]
FROM [dbo].[RoleUsers] AS [RoleUsers]
WHERE ([Users].[ID] = [RoleUsers].[UserID]) AND ([RoleUsers].[RoleID] = 1)
)

进一步简化:

SELECT
ID,
Name,
Email,
Password
FROM Users
WHERE EXISTS (SELECT
1
FROM RoleUsers
WHERE (Users.ID = RoleUsers.UserID) AND (RoleUsers.RoleID = 1)
)

  

最终,我们看到了Entity Framework使用子查询和 EXISTS 关键字来完成 Any 的操作。

当然,如果是我们自己写SQL,可以使用 IN 关键字来达到相同的效果:

SELECT
ID,
Name,
Email,
Password
FROM Users
WHERE ID IN (SELECT
UserID
FROM RoleUsers
WHERE (Users.ID = RoleUsers.UserID) AND (RoleUsers.RoleID = 1)
)

也可以使用关联查询达到相同的效果:

SELECT
ID,
Name,
Email,
Password
FROM Users
INNER JOIN RoleUsers
ON (Users.ID = RoleUsers.UserID) AND (RoleUsers.RoleID = 1)

  

幸运的是,我么只需要一个 Any 关键字就完成了这个稍微复杂的查询。  

不属于某个角色的用户列表(All的用法)

类似的,查询不属于某个角色的用户列表(用来添加用户到某个角色的UI界面中),使用Subsonic也有两种方法,我们只看下子查询的方式:

SqlQuery q = new Select().From<XUser>();
q.Where("1").IsEqualTo("1"); // 在职务名称中搜索
string searchText = ttbSearchMessage.Text.Trim();
if (!String.IsNullOrEmpty(searchText))
{
q.And(XUser.NameColumn).ContainsString(searchText);
} // 排除已经属于本角色的用户
int currentRoleId = GetQueryIntValue("id");
SqlQuery subQ = new Select(XRoleUser.UserIdColumn).From<XRoleUser>().Where(XRoleUser.RoleIdColumn).IsEqualTo(currentRoleId); q.And(XUser.IdColumn).NotIn(subQ); // 在查询添加之后,排序和分页之前获取总记录数
Grid1.RecordCount = q.GetRecordCount(); // 排列
q.OrderBys.Add(GetSortExpression(Grid1, XUser.Schema)); // 数据库分页
q.Paged(Grid1.PageIndex + 1, Grid1.PageSize);
XUserCollection items = q.ExecuteAsCollection<XUserCollection>(); Grid1.DataSource = items;
Grid1.DataBind();

  

使用Entity Framework,我们只需借助 All 关键字就能简单实现:

IQueryable<User> q = DB.Users;

// 在职务名称中搜索
string searchText = ttbSearchMessage.Text.Trim();
if (!String.IsNullOrEmpty(searchText))
{
q = q.Where(u => u.Name.Contains(searchText));
} // 排除已经属于本角色的用户
int currentRoleId = GetQueryIntValue("id");
q = q.Where(u => u.Roles.All(r => r.ID != currentRoleId)); // 在查询添加之后,排序和分页之前获取总记录数
Grid1.RecordCount = q.Count(); // 排列和分页
q = SortAndPage<User>(q, Grid1); Grid1.DataSource = q;
Grid1.DataBind();

  

可以简单的理解:检索一些用户,要保证这些用户的所有角色没有一个是currentRoleId。

  

去除分页和排序后,生成的SQL语句为:

exec sp_executesql N'SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Email] AS [Email],
[Extent1].[Password] AS [Password],
[Extent1].[Enabled] AS [Enabled],
[Extent1].[Gender] AS [Gender],
[Extent1].[ChineseName] AS [ChineseName],
[Extent1].[EnglishName] AS [EnglishName],
[Extent1].[Photo] AS [Photo],
[Extent1].[QQ] AS [QQ],
[Extent1].[CompanyEmail] AS [CompanyEmail],
[Extent1].[OfficePhone] AS [OfficePhone],
[Extent1].[OfficePhoneExt] AS [OfficePhoneExt],
[Extent1].[HomePhone] AS [HomePhone],
[Extent1].[CellPhone] AS [CellPhone],
[Extent1].[Address] AS [Address],
[Extent1].[Remark] AS [Remark],
[Extent1].[IdentityCard] AS [IdentityCard],
[Extent1].[Birthday] AS [Birthday],
[Extent1].[TakeOfficeTime] AS [TakeOfficeTime],
[Extent1].[LastLoginTime] AS [LastLoginTime],
[Extent1].[CreateTime] AS [CreateTime],
[Extent1].[DeptID] AS [DeptID]
FROM [dbo].[Users] AS [Extent1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[RoleUsers] AS [Extent2]
WHERE ([Extent1].[ID] = [Extent2].[UserID]) AND (([Extent2].[RoleID] = @p__linq__0) OR (CASE WHEN ([Extent2].[RoleID] <> @p__linq__0) THEN cast(1 as bit) WHEN ([Extent2].[RoleID] = @p__linq__0) THEN cast(0 as bit) END IS NULL))
)',N'@p__linq__0 int',@p__linq__0=1
go

  

最终简化为:

SELECT
ID,
Name,
Email,
Password
FROM Users
WHERE NOT EXISTS (SELECT
1
FROM RoleUsers
WHERE (ID = RoleUsers.UserID) AND ((RoleUsers.RoleID = 1) OR (CASE WHEN (RoleUsers.RoleID <> 1) THEN cast(1 as bit) WHEN (RoleUsers.RoleID = 1) THEN cast(0 as bit) END IS NULL))
)

  

按照我的理解,其中:

CASE WHEN (RoleUsers.RoleID <> 1) THEN cast(1 as bit) WHEN (RoleUsers.RoleID = 1) THEN cast(0 as bit) END IS NULL

类似于下面的判断:

RoleUsers.RoleID  IS NULL

只是不知道为啥会生成这么令人费解的代码。因为如果 RoleUsers.RoleID为NULL的话,既不会走进第一个WHEN,也不会走进第二个WHEN,自然就是NULL IS NULL为true了。

下面简单写个SELECT来验证我的想法:

select CASE WHEN (null <> 1)
THEN cast(1 as bit)
WHEN (null = 1)
THEN cast(0 as bit)
END

  

注意,这个NULL<>1的结果是FALSE,NULL=1的结果也是FALSE,所以最终的结果才是NULL。

再来看一个简单的SELECT查询:

  

下载或捐赠AppBox

1. AppBox v2.1 是免费软件,免费提供下载:http://fineui.com/bbs/forum.php?mod=viewthread&tid=3788

2. AppBox v3.0 是捐赠软件,你可以通过捐赠作者来获取AppBox v3.0的全部源代码(http://fineui.com/donate/)。

日寇忘我之心不死,同志尚需警惕!纪念九一八。

AppBox升级进行时 - Any与All的用法(Entity Framework)的更多相关文章

  1. AppBox升级进行时 - 拥抱Entity Framework的Code First开发模式

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 从Subsonic到Entity Framework Subsonic最早发布 ...

  2. AppBox升级进行时 - 扁平化的权限设计

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. AppBox v2.0中的权限实现 AppBox v2.0中权限管理中涉及三个 ...

  3. AppBox升级进行时 - Entity Framework的增删改查

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. Entity Framework新增数据 以新增用户为例,作为对比,先来看下使 ...

  4. AppBox升级进行时 - 关联表查询与更新(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 关联表的查询操作 使用 Include 方法,我们可以在一次数据库查询中将关联 ...

  5. AppBox升级进行时 - 如何向OrderBy传递字符串参数(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. Entity Framework提供的排序功能 再来回顾一下上篇文章,加载用户 ...

  6. AppBox升级进行时 - Attach陷阱(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. Attach方法 前面我们已经多次使用Attach方法,上一次使用Attach ...

  7. 【视频教程】一步步将AppBox升级到Pro版

    本系列教程分为上中下三部分,通过视频的形式讲解如何将基于FineUI(开源版)的AppBox v6.0一步一步升级FineUIPro(基础版). [视频教程]一步步将AppBox升级到Pro版(上)主 ...

  8. Entity Framework Core 1.1 升级通告

    原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...

  9. Entity framework在用于WCF时创建数据模型的问题

    众所周知,WCF的传输对象,在创建时需要在类名上标识[DataContract]以及在属性上标识[DataMember],当我们在使用Entity framework时(不考虑Code first的情 ...

随机推荐

  1. iOS UITableView删除cell分割线

    UITableView是UITableViewStylePlain风格的,这样整个TableView都会被分割线分隔开,不管有没有数据,非常丑. 为了可以自定义cell的分割线: 解决方案: 将UIT ...

  2. FIJ Jobs - 150316

    Department Vacancies Total Skill Set Experience Language Oracle; OBIEE 3years English Systems Sr. So ...

  3. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q121-Q123)

    Question  121 You are designing a SharePoint 2010 workflow that will be used to monitor invoices. Th ...

  4. Intent属性详解二 Action、Category

    先看效果图: 1.Action:该activity可以执行的动作 该标识用来说明这个activity可以执行哪些动作,所以当隐式intent传递过来action时,如果跟这里<intent-fi ...

  5. Windows操作系统优化(Win7版) - 进阶者系列 - 学习者系列文章

    Windows系统优化是个永恒的话题.在操作系统的打包制作方面更是有得一拼.前面提到的龙帝国社区的XP系统就是一个典型的例子,打包好的系统就已经是经过优化的,使用者无需再使用优化工具进行处理.下面就对 ...

  6. MongoDB-服务器管理

    前言 本文主要涉及一下内容:数据库的备份和恢复,对于任一数据库来说备份至关重要:数据的导入和导出:数据库修复:数据库状态监控工具mongostat:在使用过程遇到其他命令会总结在此. 1.数据库的备份 ...

  7. Windows on Device 项目实践 5 - 姿态控制灯制作

    在前面几篇文章中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成PWM调光灯.感光灯.火焰报警器和智能风扇的制作,涉及到了火焰传感器.DC直流电机. ...

  8. SQL SERVER 2012链接到SQL SERVER 2000的问题解决案例

    前几天在在桦仔的SQLSERVER走起微信公众帐号看到一篇文章MS SQL Server2014链接MS SQL Server 2000,当时手机上囫囵吞枣看了个大概,知道是由于SQL SERVER ...

  9. W3School-CSS 表格实例

    CSS 表格实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CSS 内边距 (paddi ...

  10. 安装Visual Studio的插件AnkhSvn

    安装Visual Studio的插件AnkhSvn 安装AnkhSvn(VS插件).下载地址:http://download.csdn.net/detail/luqingfei/4538807 Ank ...