I am having trouble with MVC3 framework and extracting specific data from my database:
The following ActionResult contains a string called searchString and is a Directors Name, I want to use the search string to retrieve data that matches the name and pass it to the view so that it can be displayed:
public ActionResult Index(string searchString)
{
var director = from d in db.Directors select d;
director = director.Where(searchString == d.Name);
return View(director);
}
The Directors name is stored in the database as Director.Name.
How can I retrieve the diretor who has the name == searchString?
Your query could potentially match multiple rows:
and your view could now be strongly typed to
IEnumerable<Director>:If you wanted to get only a single director you could use the FirstOrDefault method:
This will return the first row of the resultset or null if no results matched your query.