S
S
Sasha Pleshakov2016-10-06 21:30:16
C++ / C#
Sasha Pleshakov, 2016-10-06 21:30:16

Why does the compiler swear at the method in the delegate passed to FirstOrDefault?

Call

user = db.Users.FirstOrDefault(u => u.Login == loginTextBox.Text && u.PasswordHash == GetMD5Hash(passwordTextBox.Text));

Method
public static string GetMD5Hash(string str)
{            
    MD5 md5 = MD5.Create();
    byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        stringBuilder.Append(hash[i].ToString("x2"));
    }
    return stringBuilder.ToString();
}

Mistake:
ab3ae9f5b4184cc1a2c82b02abc3cfec.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2016-10-07
@mnepoh

In this case, LINQ is trying to convert a LINQ query into a server-side SQL query, and a method such as GetMD5Hash does not exist for it, so it throws an error. When converting, it does not first execute the GetMD5Hash method, with a continuation, but immediately tries to get the SQL code. Therefore, in this case, it can simply be taken out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question