Search Results for

    Show / Hide Table of Contents

    Class ExpressionsHelper

    A set of helper functions and extensions around expressions and reflection for Linq functionality

    Inheritance
    object
    ExpressionsHelper
    Inherited Members
    object.GetType()
    object.MemberwiseClone()
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    Namespace: LemonEdge.Utils
    Assembly: LemonEdge.Utils.dll
    Syntax
    public static class ExpressionsHelper

    Methods

    ChangeTo<T, TT>(Expression<Func<T, bool>>)

    Converts an expression from one type to another

    Declaration
    public static Expression<Func<TT, bool>> ChangeTo<T, TT>(this Expression<Func<T, bool>> expr) where TT : T
    Parameters
    Type Name Description
    Expression<Func<T, bool>> expr

    The expression to convert to type TT

    Returns
    Type Description
    Expression<Func<TT, bool>>

    The old expression converted to type TT

    Type Parameters
    Name Description
    T

    The original type of an expression

    TT

    The new type of the expression

    Remarks

    For example:

    public interface IMyItem
    {
        int Value { get; set; }
    }
    

    public class MyItem : IMyItem { public int Value { get; set; } public override string ToString() => Value.ToString(); }

    public class Example { public IEnumerable<MyItem> ChangeType() { Expression<Func<IMyItem, bool>> expr = x => x.Value > 10; var newExpress = expr.ChangeTo<IMyItem, bool>(); var items = new List<MyItem>() { new MyItem() { Value = 5 }, new MyItem() { Value = 15 } }; return items.Where(newExpress.Compile()); } }

    Would return: 15

    GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>>)

    Returns the PropertyInfo definition for the specified property against this type TSource

    Declaration
    public static PropertyInfo GetPropertyInfo<TSource, TProperty>(this Expression<Func<TSource, TProperty>> propertyLambda)
    Parameters
    Type Name Description
    Expression<Func<TSource, TProperty>> propertyLambda

    An expression returning the property to retrieve the PropertyInfo of

    Returns
    Type Description
    PropertyInfo

    The PropertyInfo definition for the specified property against this type TSource

    Type Parameters
    Name Description
    TSource

    The type of item this expression is against

    TProperty

    The type of the property returned from the expression

    Remarks

    For example:

    public class MyItem
    {
        public int Value { get; set; }
    }
    
    public static PropertyInfo GetPropInfo()
    {
        Expression<Func<MyItem, int>> expr = x => x.Value;
        return expr.GetPropertyInfo();
    }

    GetPropertyValue<T>(Expression<Func<T>>)

    Compiles an expression and gets the functions return value

    Declaration
    public static T GetPropertyValue<T>(this Expression<Func<T>> lambda)
    Parameters
    Type Name Description
    Expression<Func<T>> lambda

    The expression to compile

    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    The type of return value

    GetPropertyValue<In, T>(Expression<Func<In, T>>, In)

    Compiles an expression and gets the functions return value

    Declaration
    public static T GetPropertyValue<In, T>(this Expression<Func<In, T>> lambda, In input)
    Parameters
    Type Name Description
    Expression<Func<In, T>> lambda

    The expression to compile

    In input
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    In

    The input to the expression

    T

    The type of return value

    OrderByDescendingPropNameGeneric(IQueryable, Type, string)

    Orders the specified query by the specified property name in descending order

    Declaration
    public static IQueryable OrderByDescendingPropNameGeneric(this IQueryable q, Type type, string propName)
    Parameters
    Type Name Description
    IQueryable q

    The query

    Type type

    The type of item being ordered

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable

    The original query modified to be ordered in descending order by the specified property

    OrderByDescendingPropName<T>(IQueryable<T>, string)

    Orders the specified query by the specified property name in descending order

    Declaration
    public static IQueryable<T> OrderByDescendingPropName<T>(this IQueryable<T> q, string propName)
    Parameters
    Type Name Description
    IQueryable<T> q

    The query

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable<T>

    The original query modified to be ordered in descending order by the specified property

    Type Parameters
    Name Description
    T

    The type of item being ordered

    OrderByPropNameGeneric(IQueryable, Type, string)

    Orders the specified query by the specified property name in ascending order

    Declaration
    public static IQueryable OrderByPropNameGeneric(this IQueryable q, Type type, string propName)
    Parameters
    Type Name Description
    IQueryable q

    The query

    Type type

    The type of item being ordered

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable

    The original query modified to be ordered in ascending order by the specified property

    OrderByPropName<T>(IQueryable<T>, string)

    Orders the specified query by the specified property name in ascending order

    Declaration
    public static IQueryable<T> OrderByPropName<T>(this IQueryable<T> q, string propName)
    Parameters
    Type Name Description
    IQueryable<T> q

    The query

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable<T>

    The original query modified to be ordered in ascending order by the specified property

    Type Parameters
    Name Description
    T

    The type of item being ordered

    SetPropertyValue<T>(Expression<Func<T>>, T)

    Sets the underlying properties value to the given value from an expression that contains the property

    Declaration
    public static void SetPropertyValue<T>(this Expression<Func<T>> lambda, T value)
    Parameters
    Type Name Description
    Expression<Func<T>> lambda

    The expression

    T value

    The value to set the property to

    Type Parameters
    Name Description
    T

    The type of value to set

    SetPropertyValue<In, T>(Expression<Func<In, T>>, T, In)

    Sets the underlying properties value to the given value from an expression that contains the property

    Declaration
    public static void SetPropertyValue<In, T>(this Expression<Func<In, T>> lambda, T value, In input)
    Parameters
    Type Name Description
    Expression<Func<In, T>> lambda

    The expression

    T value

    The value to set the property to

    In input
    Type Parameters
    Name Description
    In

    The input to the expression

    T

    The type of value to set

    SkipGeneric(IQueryable, Type, int)

    Alters the specified query to skip the specified number of items from the result set. Usually used in conjunction with order by.

    Declaration
    public static IQueryable SkipGeneric(this IQueryable q, Type type, int count)
    Parameters
    Type Name Description
    IQueryable q

    The query to skip results from

    Type type

    The type of items in this query

    int count

    The amount of initial items to skip in the query results

    Returns
    Type Description
    IQueryable

    The original query altered to skip the specified number of initial items from the query result set

    TakeGeneric(IQueryable, Type, int)

    Alters the specified query to only take the specified number of max items from the result set. Usually used in conjunction with order by.

    Declaration
    public static IQueryable TakeGeneric(this IQueryable q, Type type, int count)
    Parameters
    Type Name Description
    IQueryable q

    The query to take a maximum number of results from

    Type type

    The type of items in this query

    int count

    The maximum amount of items to be returned by the query results

    Returns
    Type Description
    IQueryable

    The original query altered to only return the specified number of maximum items from the query result set

    ThenByDescendingPropNameGeneric(IQueryable, Type, string)

    Orders the specified query by the specified property name in descending order after an initial order has already been applied

    Declaration
    public static IQueryable ThenByDescendingPropNameGeneric(this IQueryable q, Type type, string propName)
    Parameters
    Type Name Description
    IQueryable q

    The query

    Type type

    The type of item being ordered

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable

    The original query modified to be ordered in descending order by the specified property

    ThenByDescendingPropName<T>(IQueryable<T>, string)

    Orders the specified query by the specified property name in descending order after an initial order has already been applied

    Declaration
    public static IQueryable<T> ThenByDescendingPropName<T>(this IQueryable<T> q, string propName)
    Parameters
    Type Name Description
    IQueryable<T> q

    The query

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable<T>

    The original query modified to be ordered in descending order by the specified property

    Type Parameters
    Name Description
    T

    The type of item being ordered

    ThenByPropNameGeneric(IQueryable, Type, string)

    Orders the specified query by the specified property name in ascending order after an initial order has already been applied

    Declaration
    public static IQueryable ThenByPropNameGeneric(this IQueryable q, Type type, string propName)
    Parameters
    Type Name Description
    IQueryable q

    The query

    Type type

    The type of item being ordered

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable

    The original query modified to be ordered in ascending order by the specified property

    ThenByPropName<T>(IQueryable<T>, string)

    Orders the specified query by the specified property name in ascending order after an initial order has already been applied

    Declaration
    public static IQueryable<T> ThenByPropName<T>(this IQueryable<T> q, string propName)
    Parameters
    Type Name Description
    IQueryable<T> q

    The query

    string propName

    The property we want to sort the query by

    Returns
    Type Description
    IQueryable<T>

    The original query modified to be ordered in ascending order by the specified property

    Type Parameters
    Name Description
    T

    The type of item being ordered

    Where<T>(string, SQLOperator, object, bool, bool)

    Returns an expression that evaluates the specified where clause

    Declaration
    public static Expression<Func<T, bool>> Where<T>(string propName, SQLOperator op, object value, bool odataQuery, bool isCaseInsentive = false)
    Parameters
    Type Name Description
    string propName

    The name of the property to compare in the expression

    SQLOperator op

    The operation to perform to match the property and value in the expression

    object value

    The value to compare to the property in the expression

    bool odataQuery

    Indicates if the expression is running thorough an odata service - if it is the results are assumed to be case insensitive already

    bool isCaseInsentive
    Returns
    Type Description
    Expression<Func<T, bool>>

    An expression that is the equivalent of this filter

    Type Parameters
    Name Description
    T

    The type this expression works against

    Remarks

    For example:

    public class MyItem
    {
        public int Value { get; set; }
    }
    
    public class Example
    {
        public void GetExpression()
        {
            var expr = ExpressionsHelper.Where<MyItem>(nameof(MyItem.Value), Database.SQLOperator.Equals, 5, odataQuery: false);
            //Is equivalent to the following:
            Expression<Func<MyItem, bool>> expr2 = x => x.Value == 5;
        }
    }

    Where<T>(string, string, SQLOperator, bool, bool)

    Returns an expression that evaluates the specified where clause

    Declaration
    public static Expression<Func<T, bool>> Where<T>(string propName, string propName2, SQLOperator op, bool odataQuery, bool isCaseInsentive = false)
    Parameters
    Type Name Description
    string propName

    The name of the property to compare in the expression

    string propName2

    The name of the other property to compare in the expression

    SQLOperator op

    The operation to perform to match the property and value in the expression

    bool odataQuery

    Indicates if the expression is running thorough an odata service - if it is the results are assumed to be case insensitive already

    bool isCaseInsentive
    Returns
    Type Description
    Expression<Func<T, bool>>

    An expression that is the equivalent of this filter

    Type Parameters
    Name Description
    T

    The type this expression works against

    Remarks

    For example:

    public class MyItem
    {
        public int Value { get; set; }
        public int Value2 { get; set; }
    }
    
    public class Example
    {
        public void GetExpression()
        {
            var expr = ExpressionsHelper.Where<MyItem>(nameof(MyItem.Value), nameof(MyItem.Value2), Database.SQLOperator.Equals, odataQuery: false);
            //Is equivalent to the following:
            Expression<Func<MyItem, bool>> expr2 = x => x.Value == x.Value2;
        }
    }
    In this article
    Back to top © LemonEdge Technologies. All rights reserved.