Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@khalidabuhakmeh
Created November 6, 2012 15:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khalidabuhakmeh/4025344 to your computer and use it in GitHub Desktop.
Save khalidabuhakmeh/4025344 to your computer and use it in GitHub Desktop.
RavenDB If extension method
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment