简体   繁体   English

连接表并从两个表中获取数据

[英]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. 我下面发布的当前代码,仅在我要检索ImagePath字段时才从表中返回ImageID,只是要注意这是一个单独的表,因为用户可以添加许多图像。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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