Class ExpressionsHelper
A set of helper functions and extensions around expressions and reflection for Linq functionality
Inheritance
Inherited Members
Namespace: LemonEdge.Utils
Assembly: Utils.dll
Syntax
public static class ExpressionsHelper
Methods
ChangeTo<T, TT>(Expression<Func<T, Boolean>>)
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 |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | expr | The expression to convert to type |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<TT, System.Boolean>> | The old expression converted to type |
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 System.Reflection.PropertyInfo definition for the specifed property against this type TSource
Declaration
public static PropertyInfo GetPropertyInfo<TSource, TProperty>(this Expression<Func<TSource, TProperty>> propertyLambda)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TSource, TProperty>> | propertyLambda | An expression returning the property to retrieve the PropertyInfo of |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | The System.Reflection.PropertyInfo definition for the specifed property against this type |
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();
}
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 |
---|---|---|
System.Linq.IQueryable<T> | q | The query |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.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 |
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 |
---|---|---|
System.Linq.IQueryable | q | The query |
System.Type | type | The type of item being ordered |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.IQueryable | The original query modified to be ordered in descending 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 |
---|---|---|
System.Linq.IQueryable<T> | q | The query |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.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 |
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 |
---|---|---|
System.Linq.IQueryable | q | The query |
System.Type | type | The type of item being ordered |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.IQueryable | The original query modified to be ordered in ascending order by the specified property |
SkipGeneric(IQueryable, Type, Int32)
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 |
---|---|---|
System.Linq.IQueryable | q | The query to skip results from |
System.Type | type | The type of items in this query |
System.Int32 | count | The amount of initial items to skip in the query results |
Returns
Type | Description |
---|---|
System.Linq.IQueryable | The original query altered to skip the specified number of initial items from the query result set |
TakeGeneric(IQueryable, Type, Int32)
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 |
---|---|---|
System.Linq.IQueryable | q | The query to take a maximum number of results from |
System.Type | type | The type of items in this query |
System.Int32 | count | The maximum amount of items to be returned by the query results |
Returns
Type | Description |
---|---|
System.Linq.IQueryable | The original query altered to only return the specified number of maximum items from the query result set |
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 |
---|---|---|
System.Linq.IQueryable<T> | q | The query |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.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 |
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 |
---|---|---|
System.Linq.IQueryable | q | The query |
System.Type | type | The type of item being ordered |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.IQueryable | The original query modified to be ordered in descending 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 |
---|---|---|
System.Linq.IQueryable<T> | q | The query |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.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 |
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 |
---|---|---|
System.Linq.IQueryable | q | The query |
System.Type | type | The type of item being ordered |
System.String | propName | The property we want to sort the query by |
Returns
Type | Description |
---|---|
System.Linq.IQueryable | The original query modified to be ordered in ascending order by the specified property |
Where<T>(String, SQLOperator, Object, Boolean)
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)
Parameters
Type | Name | Description |
---|---|---|
System.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 |
System.Object | value | The value to compare to the property in the expression |
System.Boolean | odataQuery | Indicates if the expression is running thorough an odata service - if it is the results are assumed to be case insensitive already |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | An expression that is the equivelant 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 equivelant to the following:
Expression<Func<MyItem, bool>> expr2 = x => x.Value == 5;
}
}
Where<T>(String, String, SQLOperator, Boolean)
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)
Parameters
Type | Name | Description |
---|---|---|
System.String | propName | The name of the property to compare in the expression |
System.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 |
System.Boolean | odataQuery | Indicates if the expression is running thorough an odata service - if it is the results are assumed to be case insensitive already |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | An expression that is the equivelant 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 equivelant to the following:
Expression<Func<MyItem, bool>> expr2 = x => x.Value == x.Value2;
}
}