user473104
user473104

Reputation: 311

Divide LINQ to Entity queries in to different methods

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");" 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;
    }

Upvotes: 0

Views: 140

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500055

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);
}

Upvotes: 1

Related Questions