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

Suggestion to add NotIn operator #36

Closed
HowardHoulston opened this issue Sep 17, 2018 · 1 comment
Closed

Suggestion to add NotIn operator #36

HowardHoulston opened this issue Sep 17, 2018 · 1 comment

Comments

@HowardHoulston
Copy link

HowardHoulston commented Sep 17, 2018

Excellent code - thank you for sharing it.

I notice there is an In operator but unless I'm missing something, no NotIn operator.

I implemented as follows:

namespace ExpressionBuilder.Operations
{
    /// <summary>
    /// Operation representing a list "Contains" method call = false.
    /// </summary>
    public class NotIn : OperationBase
    {
        /// <inheritdoc />
        public NotIn()
            : base("NotIn", 1, TypeGroup.Default | TypeGroup.Boolean | TypeGroup.Date | TypeGroup.Number | TypeGroup.Text, true, true) { }

        /// <inheritdoc />
        public override Expression GetExpression(MemberExpression member, ConstantExpression constant1, ConstantExpression constant2)
        {
            if (!(constant1.Value is IList) || !constant1.Value.GetType().IsGenericType)
            {
                throw new ArgumentException("The 'NotIn' operation only supports lists as parameters.");
            }

            var type = constant1.Value.GetType();
            var inInfo = type.GetMethod("Contains", new[] { type.GetGenericArguments()[0] });
            var contains = Expression.Call(constant1, inInfo, member);
            return Expression.Not(contains);
        }
    }
}

And adding to the Operation class:

/// <summary>
/// Operation representing a list "Contains" method call = false.
/// </summary>
public static IOperation NotIn { get { return new NotIn(); } }

I hope this helps. Forgive me - I've never done a pull / submission so don't know the process.

Regards

@dbelmont
Copy link
Owner

Hi @HowardHoulston,
Many thanks for using the project and for your contribution! 😄 (and sorry for the late reply 😅)
I'll include this in the next release and make myself sure I mention your name in the commit message and the release notes to give you the due credit.
Many thanks.

dbelmont added a commit that referenced this issue Jan 16, 2019
… - issue #36)

• Fixed issue #37 (exception thrown when using the `In` operator over a nullable property)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants