Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MongoRepository;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- class Program
- {
- static void Main(string[] args)
- {
- MongoRepository<Foo> repo = new MongoRepository<Foo>();
- var reporesult = repo.All().Where(IsInSet(new[] { 1, 4 }, 20)).ToArray();
- }
- private static Expression<Func<Foo, bool>> IsInSet(IEnumerable<int> seeds, int setsize)
- {
- if (seeds == null)
- throw new ArgumentNullException("s");
- if (!seeds.Any())
- throw new ArgumentException("No sets specified");
- return seeds.Select<int, Expression<Func<Foo, bool>>>(seed => x => x.Seed % setsize == seed).JoinByOr();
- }
- }
- public class Foo : Entity
- {
- public int Seed { get; set; }
- }
- public static class Extensions
- {
- public static Expression<Func<T, bool>> JoinByOr<T>(this IEnumerable<Expression<Func<T, bool>>> filters)
- {
- var firstFilter = filters.First();
- var body = firstFilter.Body;
- var param = firstFilter.Parameters.ToArray();
- foreach (var nextFilter in filters.Skip(1))
- {
- var nextBody = Expression.Invoke(nextFilter, param);
- body = Expression.Or(body, nextBody);
- }
- return Expression.Lambda<Func<T, bool>>(body, param);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement