繁体   English   中英

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

[英]Join tables and get data from both of them

我一直在尝试从已加入主用户表的表中获取数据,第二个表用于保存图像。 我下面发布的当前代码,仅在我要检索ImagePath字段时才从表中返回ImageID,只是要注意这是一个单独的表,因为用户可以添加许多图像。

这些是模型:

[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; }
}

控制器:

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

我假设我在模型设置中出错了。 有人可以帮忙吗?

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