简体   繁体   中英

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?

I am attempting to create a conditional statment in ASP.NET MVC 4 and Entity Framework. I need to create a list of models where the ID from one table is equal to data in another model's table. How do I right this conditional statement using Linq? Below is the code that I have so far:

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

I need to only add the "Request model to the ViewData if the id of the Request table is equal to the value of RequestID in the Job table. Note: The two columns are linked in the SQL DB.

You can try like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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