-
Notifications
You must be signed in to change notification settings - Fork 254
Closed
Description
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.
Metadata
Metadata
Assignees
Labels
No labels