LinqExpressionParser 0.1.0
dotnet add package LinqExpressionParser --version 0.1.0
NuGet\Install-Package LinqExpressionParser -Version 0.1.0
<PackageReference Include="LinqExpressionParser" Version="0.1.0" />
<PackageVersion Include="LinqExpressionParser" Version="0.1.0" />
<PackageReference Include="LinqExpressionParser" />
paket add LinqExpressionParser --version 0.1.0
#r "nuget: LinqExpressionParser, 0.1.0"
#:package LinqExpressionParser@0.1.0
#addin nuget:?package=LinqExpressionParser&version=0.1.0
#tool nuget:?package=LinqExpressionParser&version=0.1.0
LinqExpressionParser
LinqExpressionParser is class library project that you can use in your app to parse System.Linq.Expressions.Expression from string
Dependencies
- .NET 7.0 ~ 8.0
Getting start
ISegmentParser segmentParser = new SegmentParser(NullLogger<SegmentParser>.Instance);
IExpressionParser expressionParser = new ExpressionParser(NullLogger<ExpressionParser>.Instance, Options.Create<ExpressionParser>(...));
ValueSegment segment = segmentParser.ParseValue("Substring(Name, 5)");
Expression<Func<User, string>> exp = expressionParser.ParseValueExpression<User, string>(segment);
public class User
{
public string Name { get; set; }
}
First, SegmentParser parse segment from string. And ExpressionParse parse LambdaExpression from parsed segment
Version history
- 0.1
- Initial release
Parsing
SegmentParser
A class that parse string to ValueSegment or SelectorSegment.
ILogger<SegmentParser> logger = ...;
SegmentParser segmentParser = new(logger)
//ConstantSegment: "ABC\n\t"
ValueSegment stringSegment = segmentParser.ParseValue("ABC\\n\\t");
//ConstantSegment: 1.235
ValueSegment doubleSegment = segmentParser.ParseValue("1.235");
//PropertySegment
ValueSegment propertySegment = segmentParser.ParseValue("Id");
//MethodSegment: [Name: Substring, Arg: Name, 3]
ValueSegment methodSegment = segmentParser.ParseValue("Substring(Name, 3)");
//LambdaMethodSegment: [Name: Any, Parameter: Items:i, Arg: i.Price NEQ 6]
ValueSegment lambdaMethodSegment = segmentParser.ParseValue("Any(Items:i, i.Price NEQ 6)");
//OperationSegment: [Id, GTE, 6]
ValueSegment idGTESixSegment = segmentParser.ParseValue("Id GTE 6");
//OperationSegment: [IsExist EQ (Contains(Name, 'a'))]
ValueSegment complexSegment = segmentParser.ParseValue("IsExist EQ (Contains(Name, 'a'))");
//SelectorSegment
SelectorSegment selectorSegment = segmentParser.ParseSelector("IsIdGTFive = Id GT 5, Name, Items");
Constant
- A constant value
- type: int, double, string, bool, null
- bool, null value must be upper case
- string value can contain escape character
- allowed escape character: \, \', \a, \b, \f, \n, \r, \t, \v
- ex) "56", "12.345", "'abc\r\n'", "TRUE", "FALSE", "NULL"
Property
- A name of property
- if property name is 'NULL', 'TRUE', operator name, keyword or number, must start with '\'
- ex) "\TRUE", "\NULL", "\2354", "\123.456"
Method
- A method may contain arguments
- argument can be constant, property, method, lambdaMethod, parantheses, operation
- ex) "Contains(FirstName, Substring(LastName, '3'))"
LambdaMethod:
- A lambda method that declared parameter and may contain arguments
- argument can be constant, property, method, lambdaMethod, parantheses, operation
- parameter must declared by IEnumerable<T> type property
- ex) "Any(Items:i, i.Price GTE 5)"
Operator:
- A name of operator
- Add, Subtract, Divide, Multiply: "+", "-", "/", "*"
- EQ, NQ, GT, GTE, LT, LTE, And, Or
- see EOperator
Parantheses:
- A value of single segment or operation
- ex) "(Count(Items:i))", "Id GTE 6 AND (Price LTE 100 OR Price GTE 1000)"
Operation:
- ex) "123 + 456 - Name.Length"
see SegmentTest for more sample.
ExpressionParser
A class that parse Segment to LambdaExpression
ILogger<ExpressionParser> logger = ...;
ExpressionParserOptions options = new()
{
MethodMapOptions = MethodMapOptions.Default
OperatorMapOptions = OperatorMapOptions.Default
}
ExpressionParser expressionParser = new(logger, options);
//u => u.Items.Any(i => i.Price != 6)
Expression<Func<User, bool>> itemsAnyExpression = expressionParser.ParseValueExpression<User, bool>(lambdaMethodSegment);
//u => u.Id >= 6
Expression<Func<User, bool>> idGTESixExpression = expressionParser.ParseValueExpression<User, bool>(idGTESixSegment);
//u => new { IsIdGTFive = u.Id > 5, Name, Items }
LambdaExpression selectorExpression = expressionParser.ParseSelectorExpression<User>(selectorSegment);
You can configure MethodMapOptions, OperatorMapOptions. Throwing exception in delegate is not recommend
MethodMapOptions methodMapOptions = MethodMapOptions.Default;
methodMapOptions.MethodMap.Remove("Substring");
methodMapOptions.MethodMap["AVG"].Add(args => {
//Check args and return null or Expression
});
methodMapOptions.MethodMap.Add("SomeMethod", new List<GetMethodCallExpressionDelegate>()
{
args =>
{
//Check args and return null or Expression
},
args =>
{
//Check args and return null or Expression
}
});
OperatorMapOptions operatorMapOptions = OperatorMapOptions.Default;
operatorMapOptions.OperatorMap[EOperator.EQ].Add((left, right) => {
//Check args and return null or Expression
});
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0 && < 8.0.0)
- Microsoft.Extensions.Options (>= 7.0.0 && < 8.0.0)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0 && < 9.0.0)
- Microsoft.Extensions.Options (>= 8.0.0 && < 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LinqExpressionParser:
| Package | Downloads |
|---|---|
|
LinqExpressionParser.AspNetCore
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 227 | 1/16/2024 |