Class PredicateBuilder
Helper functions for combining Linq expressions through logical operators
Inheritance
Inherited Members
Namespace: LemonEdge.Utils
Assembly: Utils.dll
Syntax
public static class PredicateBuilder
Methods
And<T>(Expression<Func<T, Boolean>>, Expression<Func<T, Boolean>>)
Combines the expr1
and expr2
logical expression into one expression that is true if both of the expressions are true
Declaration
public static Expression<Func<T, bool>> And<T>(Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | expr1 | The first logical expression to be combined |
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | expr2 | The second logical expression to be combined |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | A new logical expression that returns true if |
Type Parameters
Name | Description |
---|---|
T | The type being evaluated in the expressions |
Contains<TElement, TValue>(Expression<Func<TElement, TValue>>, IEnumerable<TValue>)
Creates an expression that returns true if the item has a value that is contained in the list of hardcoded values
Declaration
public static Expression<Func<TElement, bool>> Contains<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TElement, TValue>> | valueSelector | An expression of a function that given a |
System.Collections.Generic.IEnumerable<TValue> | values | A list of values to be compared against to see if an item has a value contained in this list |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<TElement, System.Boolean>> | An expression that returns true if an item contains a value within the |
Type Parameters
Name | Description |
---|---|
TElement | The item type this expression is used against |
TValue | The type of the value returned from an instance of the |
Remarks
For example:
public class MyItem
{
public int Value { get; set; }
public string Label { get; set; }
public override string ToString() => Label;
}
public static IEnumerable<MyItem> Test()
{
var myList = new List<MyItem> { new MyItem { Value = 1, Label = "One" }, new MyItem { Value = 2, Label = "Two" }, new MyItem { Value = 3, Label = "Three" } };
var where = PredicateBuilder.Contains((MyItem x) => x.Value, new int[] { 1, 2 });
return myList.Where(where.Compile());
}
Would return:
One, Two
False<T>()
Declaration
public static Expression<Func<T, bool>> False<T>()
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> |
Type Parameters
Name | Description |
---|---|
T |
NotContains<TElement, TValue>(Expression<Func<TElement, TValue>>, IEnumerable<TValue>)
Creates an expression that returns true if the item has a value that is not contained in the list of hardcoded values
Declaration
public static Expression<Func<TElement, bool>> NotContains<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TElement, TValue>> | valueSelector | An expression of a function that given a |
System.Collections.Generic.IEnumerable<TValue> | values | A list of values to be compared against to see if an item has a value not contained in this list |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<TElement, System.Boolean>> | An expression that returns true if an item contains a value not found within the |
Type Parameters
Name | Description |
---|---|
TElement | The item type this expression is used against |
TValue | The type of the value returned from an instance of the |
Remarks
For example:
public class MyItem
{
public int Value { get; set; }
public string Label { get; set; }
public override string ToString() => Label;
}
public static IEnumerable<MyItem> Test()
{
var myList = new List<MyItem> { new MyItem { Value = 1, Label = "One" }, new MyItem { Value = 2, Label = "Two" }, new MyItem { Value = 3, Label = "Three" } };
var where = PredicateBuilder.Contains((MyItem x) => x.Value, new int[] { 1, 2 });
return myList.Where(where.Compile());
}
Would return:
Three
Or<T>(Expression<Func<T, Boolean>>, Expression<Func<T, Boolean>>)
Combines the expr1
and expr2
logical expression into one expression that is true if either of the expressions are true
Declaration
public static Expression<Func<T, bool>> Or<T>(Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | expr1 | The first logical expression to be combined |
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | expr2 | The second logical expression to be combined |
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> | A new logical expression that returns true if either |
Type Parameters
Name | Description |
---|---|
T | The type being evaluated in the expressions |
True<T>()
Declaration
public static Expression<Func<T, bool>> True<T>()
Returns
Type | Description |
---|---|
System.Linq.Expressions.Expression<System.Func<T, System.Boolean>> |
Type Parameters
Name | Description |
---|---|
T |