Home > Back-end >  Can the repository pattern fetch of data be filtered before returning results to the controller?
Can the repository pattern fetch of data be filtered before returning results to the controller?

Time:12-20

This project is C# VS-2022 Blazor WASM with REST-API repository pattern for the database API.

I keep getting a compile error whenever I use a Where() condition in repository-functions.

Error CS1061 'DbSet' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?)

For example in a repository function:

returnRecs = (await appDbContext.MOTrip).Where(r => (r.UID_CUSTOMER == uidModel));

The reason I am trying to filter in the repository is because the DB-table 'MoTrip' contains 10's-of-thousands of records. I am thinking that getting ALL records (await appDbContext.MOTrip) followed by various filtering conditions in the controller would be wasteful.

In the case presented in this question, filtering by CUSTOMER would be 1/100 the number of records fetched by the repository-function.

Your answers and comments are welcome. Thanks John.

CodePudding user response:

I would rather filter in the database rather than in memory, which will avoid pulling all 10000 records from the database. You can do that by applying .Where to the DbSet:

returnRecs = await appDbContext.MOTrip.Where(r => r.UID_CUSTOMER == uidModel)
                                      .ToListAsync();
  • Related