我正在使用c#创建登录表单。当我登录时,我收到以下错误:
LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为商店表达式。**
using (var db = new StageCraftEntities())
{
var isValidUsert = db.Users.Where(i => i.UserName == this.txtUserName.Text.ToString() && i.Password == this.txtPassword.Text.ToString()).FirstOrDefault();
var isValidUser = from User in db.Users
where User.UserName.Equals(this.txtUserName.Text.Trim().ToString())
&& User.Password.Equals(this.txtPassword.Text.Trim().ToString())
select User;
if(true)
{
Dashboard dashboard = new Dashboard(isValidUsert);
dashboard.Show();
this.Hide();
}
}
答案 0 :(得分:2)
正是它所说的,它无法将ToString转换为有效的SQL表达式。
首先获取用户名和密码字符串。
var username = this.txtUserName.Text.ToString();
var password = this.txtPassword.Text.ToString();
然后在LINQ中使用这些:
var isValidUsert = db.Users
.Where(i => i.UserName == username && i.Password == password )
.FirstOrDefault();
答案 1 :(得分:0)
将其保存到变量并检查是否相等
var username = this.txtUserName.Text.Trim().ToString();
var password = this.txtPassword.Text.Trim().ToString();
var isValidUser = from User in db.Users
where User.UserName == username
&& User.Password == password
select User;