LINQ Include on a one-to-many with .Where() not working?

前端 未结 2 1146
北恋
北恋 2020-12-21 10:09

I\'m trying to query Users including each user\'s Interests, but only where an Interest meets certain criteria:

  return db.Users.Include(u => u.Interest         


        
2条回答
  •  不知归路
    2020-12-21 10:52

    To include only some of the interests you won't be able to use the Include method, as it doesn't support this. You'll need to manually join the Interests with the Users:

    var query = from user in db.Users
        join interest in db.Interests.Where(s => s.TenantId == tenantId)
        on user.InterestId equals interest.Id //todo:  will need to be updated
        into interests;
        select new { user, interests};
    

提交回复
热议问题