You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So far, the selection middleware is working well for most of my needs. One exception is where a query is designed to return a single item, but still wanting to use the selection middleware to limit the data retrieved, which is especially useful when the item contains multiple list properties.
I can use the following inline middleware immediately before the UseSelection middleware to make this work with any IQueryable as well as query providers supporting IAsyncEnumerable without taking a dependency on EntityFrameworkCore. This is based on the idea of SingleOrDefault, but a FirstOrDefault version can easily be derived.
.Use(next =>async ctx =>{awaitnext(ctx);if(ctx.ResultisIQueryable<Profile>profilesQuery){if(profilesQuery.Take(2)isIAsyncEnumerable<Profile>profiles){varfoundProfile=false;Profile?currentProfile=default;awaitforeach(varprofileinprofiles){if(foundProfile)thrownewInvalidOperationException("Sequence contains more than one element");foundProfile=true;currentProfile=profile;}ctx.Result=currentProfile;}else{ctx.Result=profilesQuery.SingleOrDefault();}}})
The above code can be simplified if willing to take a dependency on System.Linq.Async. I'd be hesitant in a generic sense of taking a dependency on EntityFrameworkCore, as I believe this should be designed to work with any IQueryable.
The text was updated successfully, but these errors were encountered:
So far, the selection middleware is working well for most of my needs. One exception is where a query is designed to return a single item, but still wanting to use the selection middleware to limit the data retrieved, which is especially useful when the item contains multiple list properties.
I can use the following inline middleware immediately before the UseSelection middleware to make this work with any IQueryable as well as query providers supporting IAsyncEnumerable without taking a dependency on EntityFrameworkCore. This is based on the idea of SingleOrDefault, but a FirstOrDefault version can easily be derived.
The above code can be simplified if willing to take a dependency on System.Linq.Async. I'd be hesitant in a generic sense of taking a dependency on EntityFrameworkCore, as I believe this should be designed to work with any IQueryable.
The text was updated successfully, but these errors were encountered: