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 request: Match method #3699

Closed
simon-curtis opened this issue Jul 16, 2020 · 2 comments
Closed

Linq request: Match method #3699

simon-curtis opened this issue Jul 16, 2020 · 2 comments

Comments

@simon-curtis
Copy link

Simple proposal to return both matched and unmatched results from a where query in a one-liner.

var chars = new char[] { 'a', 'b', 'c' };

(matched, unmatched) = chars.Match(c => c == 'a');
for (var element in unmatched) ...

//or

IMatchResults results = chars.Match(c => c == 'a');
for (var element in results.Unmatched) ...

For readability I would normally do the following; but requires and extra pass

var chars = new char[] { 'a', 'b', 'c' };
var matched = chars.Where(c => c == 'a');
var unmatched = chars.Where(c => c != 'a');

You can achieve this in one pass using a for loop, but goes against the point of Linq and is longer

var chars = new char[] { 'a', 'b', 'c' };
var matched = new List<char>();
var unmatched = new List<char>();
foreach (var c in chars) {
    if (c == 'a') {
        matched.Add(c);
    } else {
        unmatched.Add(c);
    }
}
@333fred
Copy link
Member

333fred commented Jul 16, 2020

Closing out. Already moved this to the right repo.

@333fred 333fred closed this as completed Jul 16, 2020
@jnm2
Copy link
Contributor

jnm2 commented Jul 16, 2020

Moved to dotnet/runtime#39443

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

3 participants