为什么这个Linq返回null?

时间:2012-11-10 20:12:16

标签: c# asp.net linq .net-4.0

我有一种方法来验证我的用户,但即使我的表中有记录,它也会返回user = null

public bool AuthenticateUser(string username, string password)
{
   var user = GetSingleRow(c => c.Email.Equals(username) && c.Password.Equals(password) && IsActive);  

   if(user !=null && user.Id > 0)
   {
    // my statements
   }
}

Class&方法实施:

public partial class UserAccount :IRepository<UserAccount>
{
    private static readonly MeriRanchiDataContext Context;
    public UserAccount GetSingleRow(Func<UserAccount, bool> criteria)
    {
       return Context.UserAccounts.Where(criteria).SingleOrDefault();
       //tried this also: return Context.UserAccounts.SingleOrDefault(criteria);
    }
}

1 个答案:

答案 0 :(得分:3)

您在IsActive上缺少范围变量(c)。这样做:

var user = GetSingleRow(c => c.Email.Equals(username) && c.Password.Equals(password) && c.IsActive);