A simple tool library for converting the Expression to sql where clause
NuGet feed: https://www.nuget.org/packages/ExpressionToWhereClause/
Package | NuGet Stable | NuGet Pre-release | Downloads |
---|---|---|---|
ExpressionToWhereClause |
ExpressionToWhereClause is a NuGet library that you can add into your project that will extend your Expression<Func<TModel, bool>>
type.
It provides only one Method:
Explain of Type Expression<Func<TModel, bool>>
to the parametric
sql where clause and the parameter list
public static (string, Dictionary<string, object>) ToWhereClause<T>(this Expression<Func<T, bool>> expression, ISqlAdapter sqlAdapter = default) where T : class
The the right part of Func<TModel, bool>
must like:
[model].[PropertyName]
[comparator]
[Value]
, or the combinations.
Example:
u.Name == "Foo"
Or
u.Name == "Foo" || u.Name == "Bar"
The [Value]
can be from many places, not only the constant. For the detailed information, please see the example usage.
Warning: This library dose not support unary, like u => !(u.Name == "Foo")
, but support u => u.Name != "Foo"
and u => !u.Sex
Sex is bool type
Example usage:
See the Unit Test