尝试颠倒您的逻辑,用户名永远不会包含字符串列表,但是字符串列表可能包含用户名:
public ActionResult Index(string author= null)
{
var authors= author.Split(',');
var data = db.ComicAuthors.Where(i => authors.Contains(i.User.UserName));
return View();
}
*注意,您可能需要先提取所有 ComicAuthors,因为您可能会收到“无法将表达式转换为 SQL 表达式”类型的错误消息。
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);
}
附录
如果您想检查没有确定作者的情况,只需对作者字符串进行快速布尔检查:
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);
}