Created
November 6, 2012 15:15
-
-
Save khalidabuhakmeh/4025344 to your computer and use it in GitHub Desktop.
RavenDB If extension method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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