public
Created

RavenDB If extension method

  • Download Gist
RavenDB.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// usage
 
_db.Query<Story, Stories_Search>()
.If(status.HasValue, q => q.Where(x => x.Status == status))
.If(!curatorId.IsEmpty(), q => q.Where(x => x.CuratorId == curatorId))
.If(publicationDate.HasValue, q => q.Where(x => x.ProposedPublicationDate == publicationDate.Value.Date))
.OrderByDescending(x => x.CreatedAt)
.ToPagedList(page, size);
 
 
// extension methods
 
public static IRavenQueryable<T> If<T>(this IRavenQueryable<T> query, bool should, params Func<IRavenQueryable<T>, IRavenQueryable<T>>[] transforms)
{
return should ? transforms.Aggregate(query, (current, transform) => transform.Invoke(current)) : query;
}
 
public static IDocumentQuery<T> If<T>(this IDocumentQuery<T> query, bool should, params Func<IDocumentQuery<T>, IDocumentQuery<T>>[] transforms)
{
return should ? transforms.Aggregate(query, (current, transform) => transform.Invoke(current)) : query;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.