Skip to content

Commit 03282f9

Browse files
authored
Merge pull request #3 from Jakab-Laszlo/feature/enumerable-where-extension
Where extension for IEnumerable
2 parents 3831864 + c69dc7b commit 03282f9

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace AutSoft.Linq.Enumerable;
2+
3+
/// <summary>
4+
/// Extensions methods for <see cref="IEnumerable{T}"/> with helper functionality for Where methods
5+
/// </summary>
6+
public static class WhereExtensions
7+
{
8+
/// <summary>
9+
/// Conditionally append a where expression to <see cref="IEnumerable{T}"/> for performance considerations.
10+
/// </summary>
11+
/// <typeparam name="TSource">Element's type</typeparam>
12+
/// <param name="source">An <see cref="IEnumerable{T}" /> to conditionally filter</param>
13+
/// <param name="condition">Condition to determine which function to use in Where clause</param>
14+
/// <param name="conditionTruePredicate">Function to use if the <paramref name="condition"/> is true</param>
15+
/// <param name="conditionFalsePredicate">Optional function to use if the <paramref name="condition"/> is false</param>
16+
/// <returns><see cref="IEnumerable{T}"/> with filtering expressions.</returns>
17+
public static IEnumerable<TSource> Where<TSource>(
18+
this IEnumerable<TSource> source,
19+
bool condition,
20+
Func<TSource, bool> conditionTruePredicate,
21+
Func<TSource, bool>? conditionFalsePredicate = null)
22+
{
23+
if (condition)
24+
{
25+
return source.Where(conditionTruePredicate);
26+
}
27+
else if (conditionFalsePredicate != null)
28+
{
29+
return source.Where(conditionFalsePredicate);
30+
}
31+
else
32+
{
33+
return source;
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Conditionally append statements to a <see cref="IEnumerable{T}"/> fluent call chain
39+
/// in order to keep the fluent syntax
40+
/// </summary>
41+
/// <typeparam name="TSource">Element's type</typeparam>
42+
/// <param name="source">An <see cref="IEnumerable{T}" /> to extend</param>
43+
/// <param name="condition">Condition to determine transform the source or not</param>
44+
/// <param name="transform">Function which describes the modifications on the <paramref name="source"/>.</param>
45+
/// <returns>An extended <see cref="IEnumerable{T}"/></returns>
46+
public static IEnumerable<TSource> If<TSource>(
47+
this IEnumerable<TSource> source,
48+
bool condition,
49+
Func<IEnumerable<TSource>, IEnumerable<TSource>> transform)
50+
{
51+
return condition ? transform(source) : source;
52+
}
53+
}

src/AutSoft.Linq/Queryable/WhereExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static IQueryable<TSource> Where<TSource>(
4242
/// </summary>
4343
/// <typeparam name="TSource">Element's type</typeparam>
4444
/// <param name="source">An <see cref="IQueryable{T}" /> to extend</param>
45-
/// <param name="condition">Condition to determine which expression to use in Where clause</param>
45+
/// <param name="condition">Condition to determine transform the source or not</param>
4646
/// <param name="transform">Function which describes the modifications on the <paramref name="source"/>.</param>
4747
/// <returns>An extended <see cref="IQueryable{T}"/></returns>
4848
public static IQueryable<TSource> If<TSource>(

test/AutSoft.Linq.Tests/Queryable/WhereExtensions/WhereExtensionsTests.If.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class If : WhereExtensionsTests
1212
public void Should_AppendTrueExpression()
1313
{
1414
// Arrange
15-
var subject = Enumerable.Range(1, 10).AsQueryable();
15+
var subject = System.Linq.Enumerable.Range(1, 10).AsQueryable();
1616

1717
// Act
1818
var result = subject.If(true, q => q.Where(i => i % 2 == 0)).ToList();
@@ -26,7 +26,7 @@ public void Should_AppendTrueExpression()
2626
public void Should_NotAppendTruePredicate()
2727
{
2828
// Arrange
29-
var subject = Enumerable.Range(1, 10).AsQueryable();
29+
var subject = System.Linq.Enumerable.Range(1, 10).AsQueryable();
3030

3131
// Act
3232
var result = subject.If(false, q => q.Where(i => i % 2 == 0)).ToList();

test/AutSoft.Linq.Tests/Queryable/WhereExtensions/WhereExtensionsTests.Where.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Where : WhereExtensionsTests
1212
public void Should_AppendTruePredicate()
1313
{
1414
// Arrange
15-
var subject = Enumerable.Range(1, 10).AsQueryable();
15+
var subject = System.Linq.Enumerable.Range(1, 10).AsQueryable();
1616

1717
// Act
1818
var result = subject.Where(true, x => x % 2 == 0).ToList();
@@ -26,7 +26,7 @@ public void Should_AppendTruePredicate()
2626
public void Should_NotAppendTruePredicate()
2727
{
2828
// Arrange
29-
var subject = Enumerable.Range(1, 10).AsQueryable();
29+
var subject = System.Linq.Enumerable.Range(1, 10).AsQueryable();
3030

3131
// Act
3232
var result = subject.Where(false, x => x % 2 == 0).ToList();
@@ -40,7 +40,7 @@ public void Should_NotAppendTruePredicate()
4040
public void Should_AppendFalsePredicate()
4141
{
4242
// Arrange
43-
var subject = Enumerable.Range(1, 10).AsQueryable();
43+
var subject = System.Linq.Enumerable.Range(1, 10).AsQueryable();
4444

4545
// Act
4646
var result = subject.Where(false, x => x % 2 == 0, x => x % 2 == 1).ToList();

0 commit comments

Comments
 (0)