ASP.NET MVC 4加入两个表

时间:2016-06-25 21:06:56

标签: c# mysql asp.net asp.net-mvc entity-framework

我有2个表,一个是问题,另一个是 JiraAssignee 。问题的列名为AssigneeID,JiraAssignee由AssigeeID和AssigneeName组成。我需要AssigneeName。

我尝试过查询,但不会返回JiraAssignee表中的值。

   public ActionResult COED()
    {
     var model = new SO_80.Models.Tables();
     string COED = "COED";

     model.Issue = db.Issues.Where(d => 
     model.JiraAssignee = (from d in db.Issues.Where(d => d.JiraIssueKey.Contains(COED)).ToList()
                              join j in db.JiraAssignees.ToList() on d.JiraAssigneeID equals j.ID
                              select new JiraAssignee() { DisplayName = j.DisplayName });
     return View(model);
    }

1 个答案:

答案 0 :(得分:0)

您可以使用此代码:

public ActionResult COED()
    {
     var model = new SO_80.Models.Tables();
     string COED = "COED";
     Issue result = null;
     using (Entities db = new Entities())
            {
              result = (from d in db.Issues
                        join j in db.JiraAssignees on db.Issues equals j.ID 
                        where j.JiraIssueKey.ToLower().Trim().Contains(COED)
                        select d).FirstOrDefault();
            }
     return View(result);
    }