0

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

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.