我正在尝试执行下面的代码.我的目标是检查是否存在任何具有给定电子邮件ID的用户.
var result = userDbContext.users.SqlQuery("SELECT * FROM USERS WHERE @email='@emailValue'",
new SqlParameter("@email", "email"),
new SqlParameter("@emailValue","abc@mail.com"));
//new SqlParameter("p1", existingUser.password));
if (result.Count() == 0) //getting exception here
{
ViewBag.comment = "Sorry. We can not find your credentials";
return View();
}
Run Code Online (Sandbox Code Playgroud)
但我得到例外,result.count()不知道出了什么问题.
例外情况是:
"SqlParameter已被另一个SqlParameterCollection包含"
我怎么解决这个问题?
小智 6
您只需要在 Sql 查询之后添加 ToList() 方法并删除 SqlParameter 中的@:
var result = userDbContext.users.SqlQuery("SELECT * FROM USERS WHERE
@email=@emailValue",
new SqlParameter("email", "email"),
new SqlParameter("emailValue","abc@mail.com")).ToList();
//new SqlParameter("p1", existingUser.password));
if (result.Count() == 0) //getting exception here
{
ViewBag.comment = "Sorry. We can not find your credentials";
return View();
}
Run Code Online (Sandbox Code Playgroud)
它会起作用。
Nad*_*ikh -3
你可以试试这个
var check = (from item in userDbContext.users where item.email == email select item).FirstOrDefault();
if (check == null)
{
ViewBag.comment = "Sorry. We can not find your credentials";
return View();
}
Run Code Online (Sandbox Code Playgroud)