Class PredicateBuilder
Helper functions for combining Linq expressions through logical operators
Inherited Members
Namespace: LemonEdge.Utils
Assembly: LemonEdge.Utils.dll
Syntax
public static class PredicateBuilder
Methods
AndAll<T>(IEnumerable<Expression<Func<T, bool>>>)
Declaration
public static Expression<Func<T, bool>> AndAll<T>(IEnumerable<Expression<Func<T, bool>>> exprs)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Expression<Func<T, bool>>> | exprs |
Returns
Type | Description |
---|---|
Expression<Func<T, bool>> |
Type Parameters
Name | Description |
---|---|
T |
And<T>(Expression<Func<T, bool>>, Expression<Func<T, bool>>)
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 |
---|---|---|
Expression<Func<T, bool>> | expr1 | The first logical expression to be combined |
Expression<Func<T, bool>> | expr2 | The second logical expression to be combined |
Returns
Type | Description |
---|---|
Expression<Func<T, bool>> | 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 |
---|---|---|
Expression<Func<TElement, TValue>> | valueSelector | An expression of a function that given a |
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 |
---|---|
Expression<Func<TElement, bool>> | 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 |
---|---|
Expression<Func<T, bool>> |
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 |
---|---|---|
Expression<Func<TElement, TValue>> | valueSelector | An expression of a function that given a |
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 |
---|---|
Expression<Func<TElement, bool>> | 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, bool>>, Expression<Func<T, bool>>)
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 |
---|---|---|
Expression<Func<T, bool>> | expr1 | The first logical expression to be combined |
Expression<Func<T, bool>> | expr2 | The second logical expression to be combined |
Returns
Type | Description |
---|---|
Expression<Func<T, bool>> | 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 |
---|---|
Expression<Func<T, bool>> |
Type Parameters
Name | Description |
---|---|
T |