want to be able to do something like this below. But I have a problem because the GetStudentByName method does not know what type data is.
Im able to write the "data = data.Where(s => s.name == "Some name");开发者_StackOverflow社区" without any problems. But how do i divide it into different methods?
private IQueryable GetStudents()
{
var data = from a in db.Anvandning
select a;
return data;
}
public IQueryable GetStudentByName(string name)
{
var data = GetStudents();
data = data.Where(s => s.name == name); <-- Error occurs
return data;
}
You need to use IQueryable<T>
instead of IQueryable
, e.g
private IQueryable<Student> GetStudents()
{
var data = from a in db.Anvandning
select a;
return data;
}
public IQueryable<Student> GetStudentByName(string name)
{
var data = GetStudents();
data = data.Where(s => s.name == name);
return data;
}
Note that these methods can be written more simply:
private IQueryable<Student> GetStudents()
{
return db.Anvandning.Select(x => x);
}
public IQueryable<Student> GetStudentByName(string name)
{
return GetStudents().Where(s => s.name == name);
}
精彩评论