简体   繁体   中英

Join tables and get data from both of them

I have been trying to get data from the table I have joined to the main user table, the second table is to hold images. My current code posted below, only return the ImageID from the table when I want to be retrieving the ImagePath field, just to note this is a separate table as the user can add many images.

These are the models:

[Table("accountInfo")] // Table name
public class accountInfo
{
    [Key]
    public int AccountID { get; set; }
    public int UserId { get; set; }
    public int UserIdent { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }    
    public virtual ICollection<UserImages > UserImages { get; set; }
}

[Table("UserImages")] // Table name
public class UserImages 
{
    [Key]
    public int ImageID { get; set; }
    public int AccountID { get; set; }
    public string ImagePath { get; set; }
    public string ImageDesc { get; set; }
    public int ProfileImage { get; set; }
}

Controller:

public ActionResult Index()
{
    int id = (int)WebSecurity.CurrentUserId; 
    var users = db.AccountInformation.Include(c => c.UserImages).Where(c => c.UserId == id);

    return View(users.ToList());                 
}

I am assuming I have gone wrong in the models set up. Can anyone help?

var a = db.AccountInformation.Include(c => c.UserImages.Select(x => x.AccountId)).Where(c => c.UserId == id);

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