简体   繁体   中英

Why Where clause in not working with Virtual property in LINQ

I am trying to do something like this

var users = await _dbContext.Users.AsNoTracking().AsQueryable()
    .Include(user => user.AdminRoles)
    .Where(u => u.AdminRoles.Roles.Contains("admin2022"))
    .ToListAsync();

Here the list is getting 0 results, but when I do

var users = (await _dbContext.Users.AsNoTracking().AsQueryable()
     .Include(user => user.AdminRoles).ToListAsync())
         .Where(u => u.AdminRoles.Roles.Contains("admin2022"))
         .ToList();

Then it yields all the required results. Could some one please help me how can I make the first way work ?

Try:

var users = await _dbContext.Users
    .AsNoTracking()
    .AsQueryable()
    .Include(user => user.AdminRoles.Where(x => x.Roles.Contains("admin2022")))
    .ToListAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM