user3070072
user3070072

Reputation: 620

Calling get method using linq query?

I am trying to call the following get method in web api but I am currently experiencing an compiling error - 'bool' does not contain a definition for 'password' and no extension method 'password' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) -- error

User class:

 public class User : iUser
  {
    private cdwEntities db;

    public User()
    {
        db = new cdwEntities();
    }

    public User(cdwEntities context)
    {
        db = context;
    }


    public bool Validate(string username, string password)
    {

        var query = from t in db.Trial_Try
                    join u in db.UserDetails on t.tUID equals u.uID
                    where t.tExpiryDate >= DateTime.Now &&
                    t.tPublication.Value == 163 &&
                    u.uUsername == username &&
                    u.uPassword == password
                    select u; //

        // "execute" the query
        return query.FirstOrDefault() != null; 

    }

Test class:

  public HttpResponseMessage GetValidate(string username, string password)
    {
        User layer = new User();

        var result = layer.Validate(username, password);

        ***if (result.username != null && result.password != null)***
        {

            var mes = string.Format("Success");
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, mes);
        }
        else
        {
            //var mis = string.Format("User {0} not found");
            //return Request.CreateErrorResponse(HttpStatusCode.NotFound, mis, result);
            return null;
        }

    }

Upvotes: 0

Views: 762

Answers (2)

ekad
ekad

Reputation: 14614

The problem is result is a bool and it doesn't have username or password property, so you need to replace this line

if (result.username != null && result.password != null)

with this

if (result)

Upvotes: 1

Jim Wooley
Jim Wooley

Reputation: 10408

Validate is returning a Boolean, not the result of the query. As @ekad stated, check result as a Boolean directly.

As an additional optimization, consider using .Any instead of FirstOrDefault since you don't need the hydrated result and can optimize the SQL to an Exists query rather than select top 1. Your validate method could then be:

public bool Validate(string username, string password)
{

    var query = from t in db.Trial_Try
                join u in db.UserDetails on t.tUID equals u.uID
                where t.tExpiryDate >= DateTime.Now &&
                t.tPublication.Value == 163 &&
                u.uUsername == username &&
                u.uPassword == password
                select u; //

    // "execute" the query
    return query.Any(); 

}

Upvotes: 2

Related Questions