IN style operator #133
-
Hello, I'm wondering if there is anyway of producing the following linq query. var states = new List {...}; x => states.Contains(x.State); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Hi @sjblack, Gridify doesn't have a built-in GridifyGlobalConfiguration.CustomOperators.Register(new InOperator());
var lst = new List<TestClass>() {
new(new List<int> { 1,3,5,7,9 }),
new(new List<int> { 2,4,6,8,10 })
}.AsQueryable();
var x1 = lst.ApplyFiltering("Ids#=5");
var x2 = lst.Where(q => q.Ids.Contains(5) );
// x1 and x2 are exactly the same using custom `InOperator`
// ------------
record TestClass (IList<int> Ids);
class InOperator : IGridifyOperator
{
public string GetOperator() => "#=";
public Expression<OperatorParameter> OperatorHandler()
{
return (prop, value) => ((IList)prop).Contains(Convert.ToInt32(value));
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you for the quick reply. I did give this a go, its a slightly different scenario to the example you have provided. It's more like this in sql: 'SELECT State FROM Tasks WHERE State IN (guid1, guid2)' and Linq: class Task
{
Guid State { get; set; }
}
listOfGuids = List<Guid> { guid1, guid2 }
Tasks.Where(x => listOfGuids.Contains(x.State)); If I create the custom operator as follows its doesn't seem to play ball: class InOperator : IGridifyOperator
{
public string GetOperator() => "#=";
public Expression<OperatorParameter> OperatorHandler()
{
return (prop, value) => ((IList)value).Contains(Guid.Parse(prop));
}
} Worth mentioning I used a converter on the mapper to create the list of guids from a string representation. Appreciate the help! |
Beta Was this translation helpful? Give feedback.
-
in v2.13.1, we can use custom operators for this purpose, more info #135 https://alirezanet.github.io/Gridify/guide/filtering.html#custom-operators |
Beta Was this translation helpful? Give feedback.
I've also created an issue for this #135 if you want to follow.