Query Extensions Template

Query Extension method template. This template create an extension method for each key or index on a table.


Example of a generated query extension class

public static partial class TaskExtensions
    #region Generated Extensions
    public static Task GetByKey(this IQueryable<Task> queryable, Guid id)
        if (queryable is DbSet<Task> dbSet)
            return dbSet.Find(id);

        return queryable.FirstOrDefault(q => q.Id == id);

    public static Task<Task> GetByKeyAsync(this IQueryable<Task> queryable, Guid id)
        if (queryable is DbSet<Task> dbSet)
            return dbSet.FindAsync(id);

        return queryable.FirstOrDefaultAsync(q => q.Id == id);

    public static IQueryable<Task> ByAssignedId(this IQueryable<Task> queryable, Guid? assignedId)
        return queryable.Where(q => (q.AssignedId == assignedId || (assignedId == null && q.AssignedId == null)));

    public static IQueryable<Task> ByPriorityId(this IQueryable<Task> queryable, int? priorityId)
        return queryable.Where(q => (q.PriorityId == priorityId || (priorityId == null && q.PriorityId == null)));

    public static IQueryable<Task> ByStatusId(this IQueryable<Task> queryable, int statusId)
        return queryable.Where(q => q.StatusId == statusId);



The query extension class has the following configuration that can be set in the yaml configuration file.

    generate: true
    indexPrefix: By
    uniquePrefix: GetBy
    namespace: '{Project.Namespace}.Data.Queries'
    directory: '{Project.Directory}\Data\Queries'


Flag to enable generating the output for this template. Default: true


The namespace for the class. Variables Supported


The directory location to write the source file. Variables Supported


Prefix for queries built from an index


Prefix for queries built from unique indexes


Include XML documentation for the generated class. Default: false


The query extension template has one region that is replaced on regeneration.

Generated Configuration

The Generated Configure region configures the entity type mapping.