简体   繁体   中英

What am I Doing Wrong with IQueryable<T>?

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> '"

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.

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