简体   繁体   中英

Linq Query Unable to Return Expected Result

I am consuming wcf service into Angular JS Application. but I am receiving expected result . I am doubting something wrong on Linq Query or Angular Js Application. Here What I am trying to achieve , if username and password is wrong the method should return false and starts counting attempt . If user enter wrong username or password 4 times then account is locked . Otherwise if use enter correct username or password with in 4 times then method should return true and attempt count re set to Zero ..

Here is my interface .

[OperationContract]
    [WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    //BodyStyle = WebMessageBodyStyle.WrappedRequest,
    UriTemplate = "/AuthenticateUser")]
    bool AuthenticateUser(UserLogin userLogin );  

Here is implementation of the Method ..

  public bool AuthenticateUser(UserLogin userLogin)
    {
        using (HalifaxDatabaseEntities db = new HalifaxDatabaseEntities())
        {
        var attamp = from X in db.tblUsers
                         where X.Username==userLogin.Username&& X.Password== userLogin.Password

                         select X.RetryAttempts!=4;

            if (attamp!=null )
            {
                 return true;

            }

            else
            {

                return false;
            }
        }
    }

A linq query that returns a collection (like Select or Where ) will never return null . Only queries that return a single result (like First or Single ) can.

Although it's not clear exactly how RetryAttempts is altered, a better method might be something like:

// this will return a collection will only one result - true or false
var attamp = from X in db.tblUsers
             where X.Username==userLogin.Username&& X.Password== userLogin.Password
             select X.RetryAttempts!=4;
//.Single() will give you the value of the one and only result - either true or false
return attamp.Single();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM