简体   繁体   中英

Can the repository pattern fetch of data be filtered before returning results to the controller?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM