如果来自两个不同数据库表的 2 个数据值在 ASP.NET MVC 4 中相等,我如何有条件地将列表添加到 ViewData.Model?

How do i conditionally add a list to a ViewData.Model if 2 data values from two different DB tables are equal in ASP.NET MVC 4?

我正在尝试在 ASP.NET MVC 4 和 Entity Framework 中创建条件语句。我需要创建一个模型列表,其中一个 table 的 ID 等于另一个模型 table 中的数据。如何使用 Linq 纠正此条件语句? 以下是我目前的代码:

 public ActionResult Index()
 {
      _db = new IntegrationWebDBEntities();
      //This is the statement i am having trouble with.  
      ViewData.Model = _db.Requests.Where(r => r.id == _db.Jobs.Where(j => j.RequestID)).ToList();
      return View();
 }

如果 Request table 的 ID 等于 Job table 中 RequestID 的值,我只需要将“Request 模型添加到 ViewData”。注意:两列是在 SQL 数据库中链接。

你可以这样试试:

public ActionResult Index()
{
  _db = new IntegrationWebDBEntities();

  ViewData.Model = (from r in _db.Requests
                    from j in _db.Jobs
                    where r.id == j.RequestID
                    select r).toList();
  return View();
}