0

So I have the following code:

var itemsGrouped = this.Errors.GroupBy(x => x.UniqueName).AsEnumerable();
var hasErrors = !itemsGrouped.Any((f) =>
{
    var errorCount = f.ToArray()
    .Where(x => x.ErrorCount.HasValue)
    .Count(x => x.ErrorCount.Value > 0);

    return errorCount > 2;
});

and now I want to retrieve the individual items that matched the .Any() query. How do I get only matching items?

1
  • 1
    What about using Where insted of any
    – L.B
    Aug 31, 2017 at 23:13

1 Answer 1

5

You can't directly, using the Any() function (it just returns a bool), but the .Where() function will return a filtered IEnumerable<T> which also has the Any() function.

So something like:

var itemsGrouped = this.Errors.GroupBy(x => x.UniqueName).AsEnumerable();
var invalidItems = itemsGrouped.Where((f) =>
{
    var errorCount = f.ToArray()
    .Where(x => x.ErrorCount.HasValue)
    .Count(x => x.ErrorCount.Value > 0);

    return errorCount > 2;
});

var hasErrors = !invalidItems.Any();

//Do stuff with invalidItems
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.