views:

268

answers:

1

I thought IQueryable<T> was derrived from IEnumerable<T>, so why can't I access the results of my query like a collection of records?

public bool DoLogIn(System.String strUserName, System.String strPassword)
{
    if (this.IsLoggedIn)
        return false;

    ASRDBDataContext ASRData = new ASRDBDataContext();
    IQueryable<user> CurrUser =
        from usr in ASRData.users
        where usr.userName == strUserName
        where usr.password == strPassword
        select usr;

    if (CurrUser.Count() != 1)
        return false;

    this.CurrUserID = CurrUser[0].userID;   // Error
    return true;
}

The error returned is: "Cannot apply indexing with [] to an expression of type 'System.Linq.IQueryable<user>'"

+10  A: 

IEnumerable is not indexable. Neither is IQueryable.

If you want the first item of a LINQ query, try using the First(), FirstOrDefault(), Single(), or SingleOrDefault() methods.

Randolpho