LiteralLinq is a library aiming to make original Linq function more dynamc by pure string parameter equivalent.
#Current Developing Status
Primary functions have been implemented. Contact me if any bug found or have any new requirement. I'll reply soon.
#Target Of This Project
This project is inspired by Scott Gu's blog about dynamically create expression tree against IQueryable objects.
In some scenarios, for instance, sorting datas against runtime-determined properties, original linq is too static as it's hard-coded in design time, in the end you must write a lot of boring if statement to make it seems "dynamic".
To solve the problems refered above, this project provide a way filtering or ordering a sequence with pure string parameters. You also need to notice that one trade off you must made is that any problem, such as misspelling, will only exposed in runtime.
#Downloading LiteralLinq
You can download last version here
#Building LiteralLinq from sources
Just open the project with VS2012 or above and build.
#Usage
Add reference to LiteralLinq.dll, and add using System.Linq;
in the top of source files.
Attention: Unlucky certain functions may not supported by all IQueryable provider. It's the problem of Linq, not me.
Read Examples first is recommended.
#Implemented Extension Functions
- OrderBy: Use string expression to order a sequence.
- ThenBy
- Where: Filtering sequence with string expression.
#Implemented Static Functions
- Where.CreateFilter: Create filter expression.
##OrderBy
path direction (, path direction)*
Path: it(.property or paramless function call)*
- "it": literal string represent an element in the source sequence. Self-referencing is allowed
- property: property in the source type that can be accessed
- function call: any paramless function with a return value
Direction:
- "ASC" or "DESC", this parameter is case insensitive. "ASC" if this parameter is omitted
And you can add more subsequent expressions seperated by leading comma. These subsequent expressions will treat as ThenBy expressions.
See LiteralLinqTests\Linq\OrderByThenByTests.cs
for more details.
##ThenBy
Just same as OrderBy, except that it only applies ThenBy operation to source sequence.
##Where
path op1 value@formatter (op2 path op1 value@formatter )*
Path: it(.property or paramless function call)*
- "it": literal string represent an element in the source sequence
- property: property in the source element type that can be accessed
- function call: any paramless function with a return value
Op1: Predefined operations.
- EQ: Equal, ==
- NE: Not equal, !=
- GT: Greater then, >
- LT: Less then, <
- GE: Greater then or equal, >=
- LE: Less then or equal, <=
- IN: In an array, only used against array values
- NOT IN: Not in an array, only used against array values
- LIKE: As LIKE operator in SQL, use "%" as place holder in string value's head, tail or both to search substring in the end, start or any location of the target
Value: Support two kinds of values.
- Simple value: String surround with single quotes which can be converted to the type of target property or method's return value
- Array values: Simple values seperated by comma and surround with square brackets. Only support IN and NOT IN operators
Formatter: String surround with single quotes indicating the format of the value string.
Op2: Operator that connect two sub expressions. You can use parentheses to indicate the priority of expressions.
- AND
- OR
####Default Support Value Types
- byte
- Int16
- Int32
- Int64
- sbyte
- UInt16
- UInt32
- UInt64
- float
- double
- decimal
- string
- DateTime: Default formatter is "yyyy-MM-dd"
- char
- bool
- enum: Use enum value name as value string
- Nullable value
####Custom type support
To add new type support, you need to implement
LiteralLinq.Expression.Design.ILiteralConverter
and regist this converter with
LiteralLinq.Linq.WhereExt.RegistConverter<T>(ILiteralConverter converter)
as global converter or use
LiteralLinq.Expression.Design.LiteralConverterAttribute
to decorate a class, a struct or a property.
To struct type, if you need to compare two struct value, make sure you have overrided relate operators (Such as == for equality compare).
###Example
See LiteralLinqTests\Linq\WhereExtTests.cs
for more details.
#License
MS-PL license - http://www.microsoft.com/en-us/openness/licenses.aspx