簡體   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