How come my code is showing a syntax error on this block of code
为什么我的代码在这个代码块上显示语法错误
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;
}
I want to know if the result of query is 0, if it is 0 I will close the operation or something like that. but It keeps showing an error how would I know if the result is 0? also if you have suggestions regarding my approach feel free to put it in
我想知道查询的结果是否为0,如果是0,我将关闭操作或类似的操作。但它总是显示一个错误我怎么知道结果是否为0呢?如果你对我的方法有什么建议,请随意提出来
1 个解决方案
#1
4
Calling .First()
will result in an exception if there is no data returned...
如果没有返回数据,则调用.First()将导致异常……
Calling .FirstOrDefault()
will return null
if there is no data
如果没有数据,调用.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;
}
#1
4
Calling .First()
will result in an exception if there is no data returned...
如果没有返回数据,则调用.First()将导致异常……
Calling .FirstOrDefault()
will return null
if there is no data
如果没有数据,调用.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;
}