为什么我的代码在此代码块上显示语法错误
public string getPassword()
{
DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);
var password = (from user in myDbContext.Accounts
where user.accnt_User == txtUser.Text
select user.accnt_Pass).First();
if (password == 0)
{ }
return password;
}
我想知道查询的结果是否为0,如果是0,我将关闭操作或类似的操作。但它一直显示错误我如何知道结果是否为0?如果您对我的方法有任何建议,请随意将其放入
答案 0 :(得分:4)
如果没有返回数据,则调用.First()
将导致异常......
如果没有数据
,调用.FirstOrDefault()
将返回null
public string getPassword()
{
DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);
var password = (from user in myDbContext.Accounts
where user.accnt_User == txtUser.Text
select user.accnt_Pass).FirstOrDefault();
if (password == null)
{
// no data found - do whatever is needed in that case...
}
return password;
}