This is my (simplified) code:
Controller
var query = from f in _db.Firms
where f.Name.ToLower().Contains(search.ToLower())
|| f.Keyword.ToLower().Contains(search.ToLower())
|| f.KeywordList.ToLower().Contains(search.ToLower())
select f;
User Model
public class User
{
[Key]
public int ID { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int FirmID { get; set; }
[ForeignKey("FirmID")]
public virtual Firm Firm { get; set; }
}
Firm Model
public class Firm
{
[Key]
public int ID { get; set; }
public string Keyword { get; set; }
public string KeywordList { get; set; }
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
I have a Controller and a View which does list every Firm and it’s Users from a database, which contains a certain string. This works so far. Now, I also want to list Firms, which contains a User who has a username containing the search string. How can I accomplish that?
My thought was something like this, but it doesn’t work, since the Username isn’t available here:
var query = from f in _db.Firms
where f.Name.ToLower().Contains(search.ToLower())
|| f.Keyword.ToLower().Contains(search.ToLower())
|| f.KeywordList.ToLower().Contains(search.ToLower())
|| f.Users.Username.ToLower().Contains(search.ToLower())
select f;
use a join