Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use WithSpecification on Specification<S,R> that is the result of Select #268

Closed
ryanvade opened this issue May 23, 2022 · 1 comment · Fixed by #273
Closed

Comments

@ryanvade
Copy link

If I use Select as in the following specification I am unable to use WithSpecification because the types cannot be inferred.

// Note: this is a very basic example
public sealed class AToBSpec : Specification<A, B>{public AToBSpec(){Query.Select(a => new B(){}).AsNoTracking();}}

And then attempt to use it as follows

List<B> b = context.A.WithSpecification(new AToBSpec()).ToList();

I receive the following error:

Cannot convert source type 'System.Collections.Generic.List<A>' to target type 'System.Collections.Generic.List<B>

If I include the following additional extension for WithSpecification I can make this work (well it compiles, I have not fully tested this yet). Does it make sense to include an override of WithSpecification that builds on Specification<S,R> so that you can use Select?

public static class DbSetExtensions{public static IQueryable<TRSource> WithSpecification<TSource, TRSource>(this IQueryable<TSource> source, Specification<TSource, TRSource> specification, ISpecificationEvaluator? evaluator = null) where TSource : class{evaluator = evaluator ?? SpecificationEvaluator.Default;return evaluator.GetQuery(source, specification);}}

I'm not sure if this is a good solution or if I am just missing something.

@ryanvade ryanvade changed the title Unable to use WithSpecification with Specification<S,R> that is the result of Select Unable to use WithSpecification on Specification<S,R> that is the result of Select May 23, 2022
@fiseni
Copy link
Collaborator

fiseni commented Jun 20, 2022

Hey @ryanvade,
Thanks, we'll add this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants