Search code examples
c#linqasp.net-identity

How to join tables from IdentityDbContext and ApplicationDbContext


How do I join AspNetUsers to my tables where Email is Confirmed from AspNetUsers table

Selecting users from table AspNetUsers where email is confirmed

var context = new IdentityDbContext();
var users = context.Users.Where(d => d.EmailConfirmed == true).Select(d => d.Id).ToList();

My table

ApplicationDbContext db = new ApplicationDbContext();
UserAccountListViewModel model = new UserAccountListViewModel();
model.UserAccounts = db.UserAccounts.ToList();

Solution

  • You can do as follows:

    UserAccountListViewModel model = new UserAccountListViewModel();
    
    model.UserAccounts = db.UserAccounts.Where(ua => users.Contains(ua.Id)).ToList(); // Here `ua.Id` is the column of `UserAccount` table that you are comparing against the Id of Users table.