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?
我认为IQueryable
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>
'"
返回的错误是:“无法将带有[]的索引应用于类型为'System.Linq.IQueryable
1 个解决方案
#1
IEnumerable is not indexable. Neither is IQueryable.
IEnumerable不可索引。也不是IQueryable。
If you want the first item of a LINQ query, try using the First(), FirstOrDefault(), Single(), or SingleOrDefault() methods.
如果您想要LINQ查询的第一项,请尝试使用First(),FirstOrDefault(),Single()或SingleOrDefault()方法。
#1
IEnumerable is not indexable. Neither is IQueryable.
IEnumerable不可索引。也不是IQueryable。
If you want the first item of a LINQ query, try using the First(), FirstOrDefault(), Single(), or SingleOrDefault() methods.
如果您想要LINQ查询的第一项,请尝试使用First(),FirstOrDefault(),Single()或SingleOrDefault()方法。