Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operations Any and Not Any #62

Open
dust63 opened this issue Dec 15, 2019 · 2 comments
Open

Add operations Any and Not Any #62

dust63 opened this issue Dec 15, 2019 · 2 comments

Comments

@dust63
Copy link

dust63 commented Dec 15, 2019

Hi,

I use your lib and it was very usefull. I add some operations like Any or NotAny for List type.
I think it could be interesting to add this operations to your lib.

I also add method to filter by property expression. Example:
filter.By<Student>(x=> x.Director.Name, Operations.Equal, "test");

I think it provides more flexibility for the dev to use string property description or expression property.

@dust63 dust63 changed the title Add operations Any and Not Ant Add operations Any and Not Any Feb 11, 2020
@dust63
Copy link
Author

dust63 commented Feb 11, 2020

`
///


/// If Enumerable have no elements or is null.
///

public class NotAnyOrNull : OperationBase
{

    public NotAnyOrNull() : base("NotAnyOrNull", 0, TypeGroup.Enumerable, true, true)
    {
    }

    private static MethodInfo GetAnyMethod(MemberExpression member)
    {
        var enumerableType = typeof(Enumerable);
        var anyInfo = enumerableType.GetMethods(BindingFlags.Static | BindingFlags.Public).First(m => m.Name == "Any" && m.GetParameters().Count() == 1);
        return anyInfo.MakeGenericMethod(member.Type.GetGenericArguments().First());
    }

    // This is where your operation's behaviour lives
    // In this example, we are checking if the property's day and month are the same as today's day and month
    public override Expression GetExpression(MemberExpression member, ConstantExpression constant1, ConstantExpression constant2)
    {
        return Expression.OrElse(
            Expression.Equal(member, Expression.Constant(null)),
            Expression.Not(Expression.Call(GetAnyMethod(member), member)));
    }

    public override string ToString()
    {
        return Name;
    }
}`

@dust63
Copy link
Author

dust63 commented Feb 11, 2020

` ///


/// If Enumerable have elements and not null.
///

public class AnyAndNotNull : OperationBase
{

    public AnyAndNotNull() : base("AnyAndNotNull", 0, TypeGroup.Enumerable, true, true) { }


    private static MethodInfo GetAnyMethod(MemberExpression member)
    {
        var enumerableType = typeof(Enumerable);
        var anyInfo = enumerableType.GetMethods(BindingFlags.Static | BindingFlags.Public).First(m => m.Name == "Any" && m.GetParameters().Count() == 1);
        return anyInfo.MakeGenericMethod(member.Type.GetGenericArguments().First());
    }



    // This is where your operation's behaviour lives
    // In this example, we are checking if the property's day and month are the same as today's day and month
    public override Expression GetExpression(MemberExpression member, ConstantExpression constant1, ConstantExpression constant2)
    {
        return Expression.AndAlso(
            Expression.NotEqual(member, Expression.Constant(null)),
            Expression.Call(GetAnyMethod(member), member));
    }


}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant