It provides some generic methods for applying dynamic filters along with linq queries.
$ dotnet add package Filter.DynamicFilterA package which provides some generic methods for applying filter dynamically along with linq queries.
Using dotnet cli
dotnet add package Filter.DynamicFilter;
Add using
using DynamicFilter;
Apply filter on IAsyncEnumerable
List<User> users = await dbContext.User.AsAsyncEnumerable().ApplyFilter(filter).ToListAsync();
Apply filter on IEnumerable
var filteredList = users.ApplyFilter(filter).ToList();
{
"filter":{
"condition":"Or",
"filters":[
{
"property":"Name",
"operator":"Contains",
"value":"t2"
},
{
"property":"Employees",
"operator":"Any",
"anyFilter":{
"condition":"And",
"filters":[
{
"property":"FirstName",
"operator":"Contains",
"value":"m",
"caseSensitive": true
},
{
"property":"LastName",
"operator":"Contains",
"value":"i"
}
]
}
}
]
},
"orderBy":[
{
"property":"Id",
"ascending":false
}
],
"skip": 1,
"take": 10
}
condition : Condition by which its child filters are combined. Supported conditions - "And", "Or"filters : It should be non-empty, if condition is set.property : Property name of the entityoperator : Operator name that can be applied on the specified property of the entity. Supported operators - "Equal","NotEqual","GreaterThan","LessThan","GreaterThanOrEqual","LessThanOrEqual","Between","BetweenInclusive","In","NotIn","Contains","NotContains","Any"value : It is a dynamic property. It can be null, if operator is "Any". Example values - "Hello", 123, true, "2022-04-06", [1,4,6,3], ["This","is","a","sample"], ["2022-04-06","2000-01-10"]caseSensitive : Its default value is false. It decides whether case should be considered or notAnyFilter : Filter that should be applied inside "Any" method. It is applicable only if the operator is "Any"orderBy : A list of property names by which the list should be sorted. Sorting will depend on the order of properties specified in this list. It is optionalascending : Default value is true.skip : number of records to be skipped from the ordered list. It is optionaltake : number of records to be taken from the list. It is optionalpublic class Employee
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime DateOfJoining { get; set; }
public bool IsPermanent { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public long Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public virtual List<Employee> Employees { get; set; }
}
{
"filter":{
"property":"FirstName",
"operator":"Equal",
"value":"Marcos"
}
}
{
"filter":{
"property":"Age",
"operator":"Equal",
"value":30
}
}
{
"filter":{
"property":"FirstName",
"operator":"In",
"value":[
"Marcos",
"John"
]
},
"orderBy":[
{
"property":"FirstName"
}
]
}
{
"filter":{
"condition":"Or",
"filters":[
{
"property":"Id",
"operator":"Equal",
"value":2
},
{
"property":"Id",
"operator":"Equal",
"value":1
}
]
},
"orderBy":[
{
"property":"FirstName",
"ascending":true
}
],
"skip":0,
"take":10
}
{
"filter":{
"property":"Department.Name",
"operator":"Contains",
"value":"1",
"caseSensitive":true
},
"orderBy":[
{
"property":"FirstName",
"ascending":true
}
],
"skip":0,
"take":10
}
{
"filter":{
"property":"Employees",
"operator":"Any"
},
"orderBy":[
{
"property":"Id"
}
]
}
{
"filter":{
"property":"Employees",
"operator":"Any",
"anyFilter":{
"property":"FirstName",
"operator":"Contains",
"type":"String",
"value":"a"
}
},
"orderBy":[
{
"property":"Id"
}
]
}
{
"filter":{
"property":"Employees",
"operator":"Any",
"anyFilter":{
"condition":"And",
"filters":[
{
"property":"FirstName",
"operator":"Contains",
"value":"M",
"caseSensitive":true
},
{
"property":"LastName",
"operator":"Contains",
"value":"i"
}
]
}
}
}
{
"filter":{
"condition":"Or",
"filters":[
{
"property":"Employees",
"operator":"Any",
"anyFilter":{
"condition":"And",
"filters":[
{
"property":"FirstName",
"operator":"Contains",
"value":"m"
},
{
"property":"LastName",
"operator":"Contains",
"value":"i"
}
]
}
},
{
"property":"Name",
"operator":"Contains",
"value":"t2"
}
]
},
"orderBy":[
{
"property":"Id",
"ascending":false
}
]
}
MIT
Free Package