Reputation: 1080
I have a route which looks like: ?author=dog,cat,chicken
What I'm trying to do is get the list of authors I got from the GET request and search through the Authors Table with the list I received. How will I be able to do that?
In My view I have:
public ActionResult Index(string author= null)
{
var authors= author.Split(',');
var data = db.ComicAuthors.Where(i => i.User.UserName.Contains(authors)); //ERROR HERE
return View();
}
Upvotes: 0
Views: 860
Reputation: 39807
Try reversing your logic, the user name will never contain a list of strings, however a list of string may contain the UserName:
public ActionResult Index(string author= null)
{
var authors= author.Split(',');
var data = db.ComicAuthors.Where(i => authors.Contains(i.User.UserName));
return View();
}
*Note, you may need to pull all ComicAuthors first, as you might get a "Expression cannot be converted to a SQL expression" type error message.
public ActionResult Index(string author= null)
{
var authors= author.Split(',');
var data = db.ComicAuthors.ToList().Where(i => authors.Contains(i.User.UserName));
//May want to actually send data back to view
return View(data);
}
Addendum
If you want to check for the case that there are no authors identified, just do a quick boolean check on the author string:
public ActionResult Index(string author= null)
{
var data = db.ComicAuthors.ToList()
if(!string.IsNullOrEmpty(author)){
var authors= author.Split(',');
data = data.Where(i => authors.Contains(i.User.UserName));
}
//May want to actually send data back to view
return View(data);
}
Upvotes: 4
Reputation: 63105
public ActionResult Index(string author= null)
{
if(string.IsNullOrEmpty(author))
return View(db.ComicAuthors);
var authors= author.Split(',');
return View(db.ComicAuthors.Where(i => authors.Any(a=> a == i.User.UserName)));
}
Upvotes: 1
Reputation: 54543
var data = db.ComicAuthors.Where(i => authors.Contains(i.User.UserName));
Upvotes: 1