public class PersonDN { string county; public string Country { get { ... } set { ... } } public int IsAmerican { get { return country == "USA"; } } } (...) Database.Query<PersonDN>().Where(p=>p.IsAmerican)...;
public class PersonDN { string country; public string Country { get { ... } set { ... } } static Expression<Func<PersonDN,bool>> IsAmericanExpression = p=>p.Country == "USA"; public bool IsAmerican { get { return IsAmericanExpression.Invoke(this); } } }
Database.Query<PersonDN>().Where(p=>p.Country == "USA")...;
public static class PersonLogic { public static bool IsAmerican(this PersonDN person) { return person.Country == "USA"; } }
public static class PersonLogic { static Expression<Func<PersonDN,bool>> IsAmericanExpression = p => p.Country == "USA"; public static bool IsAmerican(this PersonDN person) { return IsAmericanExpression.Invoke(person); } }
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm) { (...) dqm[PeopleQueries.NotMarried] = from p in Database.Query<PersonDN>() where p.State == MaritalStatus.Single || p.State == MaritalStatus.Divorced select new { Entity = p.ToLite(), p.Id, p.Name, p.Sex } dqm[PeopleQueries.NotMarriedAlive] = from p in Database.Query<PersonDN>() where (p.State == MaritalStatus.Single || p.State == MaritalStatus.Divorced) && p.Alive select new { Entity = p.ToLite(), p.Id, p.Name, p.Sex } }
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm) { (...) Expression<Func<PersonDN,bool>> notMarried = p => p.State == MaritalStatus.Single || p.State == MaritalStatus.Divorced; dqm[PeopleQueries.NotMarried] = from p in Database.Query<PersonDN>() where notMarried.Invoke(p) select new { Entity = p.ToLite(), p.Id, p.Name, p.Sex } dqm[PeopleQueries.NotMarriedAlive] = from p in Database.Query<PersonDN>() where notMarried.Invoke(p) && p.Alive select new { Entity = p.ToLite(), p.Id, p.Name, p.Sex } }
Expression<Func<PersonDN, ¿?>> selector = p=> new { Entity = p.ToLite(), p.Id, p.Name, p.Sex };
Expression<Func<PersonDN, var>> selector = (...)
public static class Linq { //All the methods just return f. public static Expression<Func<R>> Expr<R>(Expression<Func<R>> f) public static Expression<Func<T, R>> Expr<T, R>(Expression<Func<T, R>> f) public static Expression<Func<T0, T1, R>> Expr<T0, T1, R>(Expression<Func<T0, T1, R>> f) public static Expression<Func<T0, T1, T2, R>> Expr<T0, T1, T2, R>(Expression<Func<T0, T1, T2, R>> f) public static Expression<Func<T0, T1, T2, T3, R>> Expr<T0, T1, T2, T3, R>(Expression<Func<T0, T1, T2, T3, R>> f) public static Func<T, R> Func<T, R>(Func<T, R> f) public static Func<T0, T1, R> Func<T0, T1, R>(Func<T0, T1, R> f) public static Func<T0, T1, T2, R> Func<T0, T1, T2, R>(Func<T0, T1, T2, R> f) public static Func<T0, T1, T2, T3, R> Func<T0, T1, T2, T3, R>(Func<T0, T1, T2, T3, R> f) }
public static void Start(SchemaBuilder sb, DynamicQueryManager dqm) { (...) Expression<Func<PersonDN,bool>> notMarried = p => p.State == MaritalStatus.Single || p.State == MaritalStatus.Divorced; var selector = Linq.Expr((PersonDN p)=>new { Entity = p.ToLite(), p.Id, p.Name, p.Sex }); dqm[PeopleQueries.NotMarried] = from p in Database.Query<PersonDN>() where notMarried.Invoke(p) select selector.Invoke(p) dqm[PeopleQueries.NotMarriedAlive] = from p in Database.Query<PersonDN>() where notMarried.Invoke(p) && p.Alive select selector.Invoke(p) }
dqm[PeopleQueries.NotMarried] = Database.Query<PersonDN>() .Where(notMarried) .Select(selector); dqm[PeopleQueries.NotMarriedAlive] = Database.Query<PersonDN>() .Where(p=>notMarried.Invoke(p) && p.Alive) //Here Invoke is really necessary .Select(selector);
[MethodExpander(typeof(IsExpander))] public static bool Is<T>(this T entity1, T entity2) where T : class, IIdentifiable { (...) }
class IsExpander : IMethodExpander { public Expression Expand(Expression instance, Expression[] arguments, Type[] typeArguments) { return Expression.Equal(arguments[0], arguments[1]); } }
[MethodExpander(typeof(IsInIntervalExpander1))] public static bool IsInInterval(this DateTime date, DateTime minDate, DateTime maxDate) { return minDate <= date && date < maxDate; }class IsInIntervalExpander1 : IMethodExpander { static readonly Expression<Func<DateTime, DateTime, DateTime, bool>> func = (date, minDate, maxDate) => minDate <= date && date < maxDate; public Expression Expand(Expression instance, Expression[] arguments, Type[] typeArguments) { return Expression.Invoke(func, arguments[0], arguments[1], arguments[2]); } }
public static class ExpressionExtensions { //Invoke overloadings. Compile, cache and evaluate your expression public static T Invoke<A0, T>(this Expression<Func<A0, T>> expr, A0 a0) public static T Invoke<A0, A1, T>(this Expression<Func<A0, A1, T>> expr, A0 a0, A1 a1) public static T Invoke<A0, A1, A2, T>(this Expression<Func<A0, A1, A2, T>> expr, A0 a0, A1 a1, A2 a2) public static T Invoke<A0, A1, A2, A3, T>(this Expression<Func<A0, A1, A2, A3, T>> expr, A0 a0, A1 a1, A2 a2, A3 a3) //Returns a compiled and cached version of the expression (in case the previous overloads don't work) public static T CompileAndStore<T>(this Expression<T> expression) //Calls ExpressionToString for a nicely composable string representation of an expression tree. public static string NiceToString(this Expression expression) //Allows Extensibility over non-Signum IQueryable providers. public static IQueryable<T> ToExpandable<T>(this IQueryable<T> q) }