[英]Linq Query return error .An explict conversion is exist
我正在研究linq查詢。 我有一個名為tblUsers的表,它的列名稱為username,password,reattamp和isLocked。 使用linq查詢,我正在檢查用戶名和密碼,然后如果用戶名帳戶已鎖定,我想返回false;否則,如果用戶帳戶已解鎖並且用戶名和密碼正確,則我想方法返回true ..當出現以下錯誤時我完成查詢。
Severity Code Description Project File Line Suppression State
Error CS0266 Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?) HalifaxWCFProject
這是我在ADO.NET中的代碼和正常工作...
public bool AuthenticateUser(UserLogin userLogin)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
var result = false;
SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
cmd.CommandType = CommandType.StoredProcedure;
string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(userLogin.Password, "SHA1");
SqlParameter paramUsername = new SqlParameter("@UserName", userLogin.Username);
SqlParameter paramPassword = new SqlParameter("@Password", encryptedpassword);
cmd.Parameters.Add(paramUsername);
cmd.Parameters.Add(paramPassword);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]);
if (Convert.ToBoolean(rdr["AccountLocked"]))
{
result = false;
}
else if (RetryAttempts == 1)
{
result = false;
}
else if (RetryAttempts > 1)
{
int AttemptsLeft = (4 - RetryAttempts);
result = true;
}
else if (Convert.ToBoolean(rdr["Authenticated"]))
{
result = true;
}
}
}
return result;
}
}
這是我的代碼linq ..
public bool AuthenticateUser1(UserLogin userLogin)
{
using (HalifaxDatabaseEntities db = new HalifaxDatabaseEntities())
{
var exceeded = false;
var totalRetries = -1;
var attamp = from X in db.tblUsers
where X.Username == userLogin.Username
select X;
if (attamp.Any())
{
if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
totalRetries = attamp.FirstOrDefault().RetryAttempts;//Error on this line
exceeded = totalRetries > 4;
}
}
return exceeded;
}
}
根據錯誤消息,看起來RetryAttempts
是可為null的int。 您的代碼正在嘗試將其設置為int變量。
您可以將變量更改為可為null的int
int? totalRetries=null;
或修復代碼以包含空檢查,如果不為空,請讀取Value
屬性
if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
}
使用C#6空條件運算符可以進一步簡化代碼。 實際上,您不需要totalRetries
變量,因為它是一個臨時變量,可以得出exceeded
的布爾變量的值,這就是您的方法返回的值。
bool exceeded=false;
var attamp = db.tblUsers.FirstOrDefault(x=>x.UserName == userLogin.Username);
if (attamp?.RetryAttempts != null)
{
exceeded = attamp.RetryAttempts.Value> 4;
}
您不能存儲int?
類型的值int?
到int
類型的變量,因為根本沒有辦法將null表示為整數,因此出現錯誤; 要解決編譯器錯誤,您可以執行以下操作:
totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
要么:
totalRetries = attamp.FirstOrDefault().RetryAttempts ?? totalRetries;
RetryAttempts
似乎是可為null的int( int?
)。 你有嘗試過嗎?
if (attamp.FirstOrDefault().RetryAttempts.HasValue)
{
totalRetries = attamp.FirstOrDefault().RetryAttempts.Value;
exceeded = totalRetries > 4;
}
您可以在此處閱讀有關可空類型的更多信息: https : //docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/nullable-types/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.