c# - 检查列表包含字符串

标签 c# json linq

有一个将用于远程验证功能的类,但无法使其工作

[HttpPost]
public JsonResult doesUserNameExist(string Forename)
{
    IEnumerable<SelectListItem> user = new List<SelectListItem>();
    using (EIPInternalEntities ctx = new EIPInternalEntities())
    {
        user = new SelectList(ctx.Database
                                 .SqlQuery<string>("EXEC dbo.uspGetLkUpJobTitle")
                                 .ToList());
    }

    var userlist = user.ToList();

    //return Json(user == null);
    return Json(!userlist.Contains(Forename));
}

尝试了不同的方法,但目前 (Forename)) 被标记为错误

"Argument 1 cannot be converted from 'string' to 'System.Web.Mvc.SelectListItem'

如果我尝试

var userlist = (SelectList)user;

//return Json(user == null);
return Json(!userlist.Contains(Forename));

然后是 !userList。被标记说

SelectList does not contain a definition for Contains

最佳答案

试试这个

return Json(!userlist.Any(x => x.Text == Forename));

关于c# - 检查列表包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29845566/

相关文章:

c# - 与结构大小和性能相关的特殊结果

c# - 带左连接的 LINQ 和枚举

c# - WCF NetTcpBinding - 来自 Microsoft 教程 - 超时

c# - 没有完全理解为什么我要在类中有一个接口(interface),而不是继承它

c# - 奇怪的 Assembly.Load 错误尝试加载使用 C# 代码提供程序编译的程序集

java - 使用java将String转换为JSONArray

php - 当内容类型为 HTML 时,JSON_PRETTY_PRINT 无法按预期工作

ruby-on-rails - 如何将 Rails Active Records 对象转换为键 :name JSON pair

c# - LINQ 查询确定输入是否在列表边界内?

c# - 更新 LINQ 子句中的属性会破坏与 UI 的数据绑定(bind)