Skip to content

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

@ryanvade

Description

@ryanvade

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions