Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created August 30, 2012 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritalin/3521721 to your computer and use it in GitHub Desktop.
Save ritalin/3521721 to your computer and use it in GitHub Desktop.
Any filter extension
using System;
using System.Collections.Generic;
using System.Linq;
namespace Sample {
public static class EnumerableAnyExtensions {
public static IEnumerable<TSource> WhereAny<TSource>(this IEnumerable<TSource> inSelf, params Func<TSource, bool>[] inFilters) {
return inSelf.Where(
source => inFilters.Any(fn => fn(source))
);
}
public static int CountAny<TSource>(this IEnumerable<TSource> inSelf, params Func<TSource, bool>[] inFilters) {
return inSelf.Count(
source => inFilters.Any(fn => fn(source))
);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace Sample {
[TestFixture]
public class EnumerableTest {
[Test]
public void _2か3どちらかの倍数() {
var count = Enumerable.Range(1, 10)
.CountAny(
i => i % 2 == 0,
i => i % 3 == 0
);
Assert.That(count, Is.EqualTo(7));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment