How to select everything using IQueryable

Using MVC3, I have a student repository (in a project) and StudentService (in another project). In the service, I want to create a function that returns all students that are in the db table. This is a new way to do something for me, so I'm a little newbie. in the GetAllStudents function below, How can I change the syntax to select everything.

In the repository:

namespace SpeakOut.Data { public class StudentRepository { SpeakOutDataContext context; private Table<StudentEntity> table; public StudentRepository() { context = new SpeakOutDataContext(); table = context.GetTable<StudentEntity>(); } public IQueryable<Student> Select() { return table.Select(x => new Student { WNumber = x.WNumber, CatalogueYear = x.CatalogueYear, Standing = x.Standing }); } } 

}

In services:

  namespace SpeakOut.Services { public class StudentService { private StudentRepository repository; public StudentService() { repository = new StudentRepository(); } public IQueryable<Student> GetAllStudents() { return repository.Select().All(x => x.FirstName) ; //**This line is where I don't know how I would call all the students** } } 

}

+4
source share
2 answers
 public IQueryable<Student> GetAllStudents() { return repository.Select(); } 

The above code only shows the pass-through method. The only advantage is to hide the repository behind the service and possibly give it a better name.

No data was received at this point. Data collection is delayed until IQuerable is used, for example, when .ToList() called. The advantage of leaving it as IQuerablye is that additional filters and sort orders can be added by code further down the line. These add-ons will be used by the LINQ provider.

+2
source

You don’t even need to just empty Select, you can just call .AsQueryable or just return the β€œtable” as IQueryable.

+2
source

Source: https://habr.com/ru/post/1397469/


All Articles