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

Linq.Except with Func parameter (aka Where not) #26168

Closed
Thaina opened this issue May 15, 2018 · 3 comments
Closed

Linq.Except with Func parameter (aka Where not) #26168

Thaina opened this issue May 15, 2018 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Linq
Milestone

Comments

@Thaina
Copy link

Thaina commented May 15, 2018

When we have write complex logic in Linq and want it to inverse selection easily

var filter = array.Where((item) => {
    switch(enumValue)
    {
        case 1: return !item.Condition1;
        case 2: return !item.Condition2;
        case 3: return !item.Condition3;
    }

    if(anotherCondition)
        return !(item.Condition1 || item.Condition2 || item.Condition3);

    // more complex logic for other conditions and etc
});

Sometimes most of the logic need to be false. So it more clean to have inverse Where clause

I think Except would do. While currently it accept another enumerable as parameter. If it overload to accept func it would be useful

var filter = array.Except((item) => {
    switch(enumValue)
    {
        case 1: return item.Condition1;
        case 2: return item.Condition2;
        case 3: return item.Condition3;
    }

    if(anotherCondition)
        return item.Condition1 || item.Condition2 || item.Condition3;

    // more complex logic for other conditions and etc
});

Even a short logic could be optimized

Func<object,bool> filter;

var filtered = array.Where((item) => !filter(item));
// became
var filtered = array.Except(filter);
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Sep 2, 2020
@eiriktsarpalis
Copy link
Member

Local methods are your friend:

var filter = array.Where(item => {
    return !ComplexPredicate(item);

    bool ComplexPredicate(Foo item)
    {
        switch (enumValue)
        {
            case 1: return item.Condition1;
            case 2: return item.Condition2;
            case 3: return item.Condition3;
        }

        if (anotherCondition)
            return item.Condition1 || item.Condition2 || item.Condition3;
    }
});

@Clockwork-Muse
Copy link
Contributor

Also, note that Except specifically is one of the "set operations", and removes duplicates, which probably isn't desired behavior.

@eiriktsarpalis
Copy link
Member

I'm going to close this issue, since I don't feel it meets the bar for inclusion to System.Linq. Feel free to reopen if you would like the conversation to continue.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Linq
Projects
None yet
Development

No branches or pull requests

6 participants