public class Dept { public int Id { get; set; } public string Name { get; set; } } public class User { public int Id { get; set; } public string Name { get; set; } public int DeptId { get; set; } } public class Role { public int Id { get; set; } public string Name { get; set; } } public class RoleUser { public int RoleId { get; set; } public int UserId { get; set; } } public class Menu { public int Id { get; set; } public string Name { get; set; } } public class RoleMenu { public int RoleId { get; set; } public int MenuId { get; set; } }
public class Database : DbContext { public Database(DbContextOptions<Database> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Dept>().ToTable("Dept").HasKey(v => v.Id); modelBuilder.Entity<Dept>().Property(v => v.Name).HasMaxLength(50).IsRequired(true); modelBuilder.Entity<User>().ToTable("User").HasKey(v => v.Id); modelBuilder.Entity<User>().Property(v => v.Name).HasMaxLength(50).IsRequired(true); modelBuilder.Entity<User>().Property(v => v.DeptId).IsRequired(true); modelBuilder.Entity<Role>().ToTable("Role").HasKey(v => v.Id); modelBuilder.Entity<Role>().Property(v => v.Name).HasMaxLength(50).IsRequired(true); modelBuilder.Entity<RoleUser>().ToTable("RoleUser").HasKey(v => new { v.RoleId, v.UserId }); modelBuilder.Entity<Menu>().ToTable("Menu").HasKey(v => v.Id); modelBuilder.Entity<Menu>().Property(menu => menu.Name).HasMaxLength(50).IsRequired(true); modelBuilder.Entity<RoleMenu>().ToTable("RoleMenu").HasKey(v => new { v.RoleId, v.MenuId }); } public DbSet<Dept> Depts { get; set; } public DbSet<User> Users { get; set; } public DbSet<Role> Roles { get; set; } public DbSet<RoleUser> RoleUsers { get; set; } public DbSet<Menu> Menus { get; set; } public DbSet<RoleMenu> RoleMenus { get; set; } }
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddDbContext<Database>(options => { options.UseSqlServer(@"server=(localdb)MSSQLLocalDb;database=LINQ;trusted_connection=true;"); }); services.AddMvc(); } public void Configure(IApplicationBuilder app) { app.UseMvc(); } }
[Route("linq")] public class LinqController : Controller { private readonly Database _database; public LinqController(Database database) { _database = database; } [HttpGet] public IActionResult Get() { #region Depts var depts1 = from l in _database.Depts.AsNoTracking() select new { l.Id, l.Name, Users = from r in _database.Users.AsNoTracking() where r.DeptId == l.Id select r }; var depts2 = _database.Depts.AsNoTracking() .Select(l => new { l.Id, l.Name, Users = _database.Users.AsNoTracking() .Where(r => r.DeptId == l.Id) .Select(r => r) }); #endregion #region Users var rolesWithUserIds1 = from l in _database.RoleUsers.AsNoTracking() join r in _database.Roles.AsNoTracking() on l.RoleId equals r.Id select new { l.RoleId, l.UserId, RoleName = r.Name // Role by UserId }; var usersWithRoleIds1 = from l in _database.RoleUsers.AsNoTracking() join r in _database.Users.AsNoTracking() on l.UserId equals r.Id select new { l.RoleId, l.UserId, UserName = r.Name // User by RoleId }; var menusWithRoleIds1 = from l in _database.RoleMenus.AsNoTracking() join r in _database.Menus.AsNoTracking() on l.MenuId equals r.Id select new { l.RoleId, l.MenuId, MenuName = r.Name // Menu by RoleId }; var menusWithUserIds1 = (from l in usersWithRoleIds1 join r in menusWithRoleIds1 on l.RoleId equals r.RoleId select new { l.UserId, r.MenuId, r.MenuName // Menu by UserId }).Distinct(); var users1 = from l in _database.Users.AsNoTracking() join r in _database.Depts.AsNoTracking() on l.DeptId equals r.Id into lr // 左外连接 from r in lr.DefaultIfEmpty() select new { l.Id, l.Name, DeptId = r == null ? 0 : r.Id, DeptName = r == null ? null : r.Name, Roles = from t in rolesWithUserIds1 where t.UserId == l.Id select new { t.RoleId, t.RoleName }, Menus = from t in menusWithUserIds1 where t.UserId == l.Id select new { t.MenuId, t.MenuName } }; var rolesWithUserIds2 = _database.RoleUsers.AsNoTracking() .Join(_database.Roles.AsNoTracking(), l => l.RoleId, r => r.Id, (l, r) => new { l.RoleId, l.UserId, RoleName = r.Name // Role by UserId }); var usersWithRoleIds2 = _database.RoleUsers.AsNoTracking() .Join(_database.Users.AsNoTracking(), l => l.UserId, r => r.Id, (l, r) => new { l.RoleId, l.UserId, UserName = r.Name // User by RoleId }); var menusWithRoleIds2 = _database.RoleMenus.AsNoTracking() .Join(_database.Menus.AsNoTracking(), l => l.MenuId, r => r.Id, (l, r) => new { l.RoleId, l.MenuId, MenuName = r.Name // Menu by RoleId }); var menusWithUserIds2 = usersWithRoleIds2.Join(menusWithRoleIds2, l => l.RoleId, r => r.RoleId, (l, r) => new { l.UserId, r.MenuId, r.MenuName // Menu by UserId }).Distinct(); var users2 = _database.Users.AsNoTracking() .GroupJoin(_database.Depts.AsNoTracking(), l => l.DeptId, r => r.Id, (l, rs) => new { User = l, Depts = rs }) .SelectMany(lr => lr.Depts.DefaultIfEmpty(), (l, r) => new { l.User.Id, l.User.Name, DeptId = r == null ? 0 : r.Id, DeptName = r == null ? null : r.Name, Roles = rolesWithUserIds2.Where(t => t.UserId == l.User.Id), Menus = menusWithUserIds2.Where(t => t.UserId == l.User.Id) }); #endregion #region Roles var roles1 = from l in _database.Roles.AsNoTracking() select new { l.Id, l.Name, Menus = from t in menusWithRoleIds1 where t.RoleId == l.Id select new { t.MenuId, t.MenuName } }; var roles2 = _database.Roles.AsNoTracking() .Select(l => new { l.Id, l.Name, Menus = menusWithRoleIds2.Where(t => t.RoleId == l.Id) .Select(t => new { t.MenuId, t.MenuName }) }); #endregion #region Menus var roleWithMenuIds1 = from l in _database.RoleMenus.AsNoTracking() join r in _database.Roles.AsNoTracking() on l.RoleId equals r.Id select new { l.RoleId, l.MenuId, RoleName = r.Name // Role by MenuId }; var menus1 = from l in _database.Menus.AsNoTracking() select new { l.Id, l.Name, Roles = from t in roleWithMenuIds1 where t.MenuId == l.Id select new { t.RoleId, t.RoleName } }; var roleWithMenuIds2 = _database.RoleMenus.AsNoTracking() .Join(_database.Roles.AsNoTracking(), l => l.RoleId, r => r.Id, (l, r) => new { l.RoleId, l.MenuId, RoleName = r.Name // Role by MenuId }); var menus2 = _database.Menus.AsNoTracking() .Select(l => new { l.Id, l.Name, Menus = roleWithMenuIds2.Where(t => t.MenuId == l.Id) .Select(t => new { t.RoleId, t.RoleName }) }); #endregion return Ok(new { depts1, depts2, users1, users2, roles1, roles2, menus1, menus2 }); } }