Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public static partial class Queryable
public static System.Linq.IQueryable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<System.Func<TSource, TKey>> keySelector, System.Linq.Expressions.Expression<System.Func<TSource, TElement>> elementSelector, System.Linq.Expressions.Expression<System.Func<TKey, System.Collections.Generic.IEnumerable<TElement>, TResult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { throw null; }
public static System.Linq.IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner, System.Linq.Expressions.Expression<System.Func<TOuter, TKey>> outerKeySelector, System.Linq.Expressions.Expression<System.Func<TInner, TKey>> innerKeySelector, System.Linq.Expressions.Expression<System.Func<TOuter, System.Collections.Generic.IEnumerable<TInner>, TResult>> resultSelector) { throw null; }
public static System.Linq.IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this System.Linq.IQueryable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner, System.Linq.Expressions.Expression<System.Func<TOuter, TKey>> outerKeySelector, System.Linq.Expressions.Expression<System.Func<TInner, TKey>> innerKeySelector, System.Linq.Expressions.Expression<System.Func<TOuter, System.Collections.Generic.IEnumerable<TInner>, TResult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { throw null; }
public static System.Linq.IQueryable<(int Index, TSource Item)> Index<TSource>(this System.Linq.IQueryable<TSource> source) { throw null; }
public static System.Linq.IQueryable<TSource> IntersectBy<TSource, TKey>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TKey> source2, System.Linq.Expressions.Expression<System.Func<TSource, TKey>> keySelector) { throw null; }
public static System.Linq.IQueryable<TSource> IntersectBy<TSource, TKey>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TKey> source2, System.Linq.Expressions.Expression<System.Func<TSource, TKey>> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { throw null; }
public static System.Linq.IQueryable<TSource> Intersect<TSource>(this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2) { throw null; }
Expand Down
16 changes: 16 additions & 0 deletions src/libraries/System.Linq.Queryable/src/System/Linq/Queryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,22 @@ public static IQueryable<TSource> UnionBy<TSource, TKey>(this IQueryable<TSource
Expression.Constant(comparer, typeof(IEqualityComparer<TKey>))));
}

/// <summary>Return index and the associated item.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IQueryable{T}" /> to return an element from.</param>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
[DynamicDependency("Index`1", typeof(Enumerable))]
public static IQueryable<(int Index, TSource Item)> Index<TSource>(this IQueryable<TSource> source)
{
ArgumentNullException.ThrowIfNull(source);

return source.Provider.CreateQuery<(int Index, TSource Item)>(
Expression.Call(
null,
new Func<IQueryable<TSource>, IQueryable<(int Index, TSource Item)>>(Index).Method,
source.Expression));
}

[DynamicDependency("Intersect`1", typeof(Enumerable))]
public static IQueryable<TSource> Intersect<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2)
{
Expand Down
26 changes: 26 additions & 0 deletions src/libraries/System.Linq.Queryable/tests/IndexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.Linq.Tests
{
public class IndexTests : EnumerableBasedTests
{
[Fact]
public void ThrowsOnNullSource()
{
IQueryable<int> source = null;
AssertExtensions.Throws<ArgumentNullException>("source", () => source.Index());
}

[Fact]
public void Index()
{
string[] source = ["a", "b"];
var actual = source.AsQueryable().Index();
(int Index, string Item)[] expected = [(0, "a"), (1, "b")];
Assert.Equal<(int Index, string Item)>(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="FirstTests.cs" />
<Compile Include="GroupByTests.cs" />
<Compile Include="GroupJoinTests.cs" />
<Compile Include="IndexTests.cs" />
<Compile Include="IntersectTests.cs" />
<Compile Include="JoinTests.cs" />
<Compile Include="LastOrDefaultTests.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Linq/ref/System.Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static System.Collections.Generic.IEnumerable<
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this System.Collections.Generic.IEnumerable<TSource> source, System.Func<TSource, TKey> keySelector, System.Func<TSource, TElement> elementSelector, System.Func<TKey, System.Collections.Generic.IEnumerable<TElement>, TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { throw null; }
public static System.Collections.Generic.IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this System.Collections.Generic.IEnumerable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner, System.Func<TOuter, TKey> outerKeySelector, System.Func<TInner, TKey> innerKeySelector, System.Func<TOuter, System.Collections.Generic.IEnumerable<TInner>, TResult> resultSelector) { throw null; }
public static System.Collections.Generic.IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this System.Collections.Generic.IEnumerable<TOuter> outer, System.Collections.Generic.IEnumerable<TInner> inner, System.Func<TOuter, TKey> outerKeySelector, System.Func<TInner, TKey> innerKeySelector, System.Func<TOuter, System.Collections.Generic.IEnumerable<TInner>, TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { throw null; }
public static System.Collections.Generic.IEnumerable<(int Index, TSource Item)> Index<TSource>(this System.Collections.Generic.IEnumerable<TSource> source) { throw null; }
public static System.Collections.Generic.IEnumerable<TSource> IntersectBy<TSource, TKey>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TKey> second, System.Func<TSource, TKey> keySelector) { throw null; }
public static System.Collections.Generic.IEnumerable<TSource> IntersectBy<TSource, TKey>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TKey> second, System.Func<TSource, TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { throw null; }
public static System.Collections.Generic.IEnumerable<TSource> Intersect<TSource>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second) { throw null; }
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Linq/src/System.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="System\Linq\First.cs" />
<Compile Include="System\Linq\Grouping.cs" />
<Compile Include="System\Linq\GroupJoin.cs" />
<Compile Include="System\Linq\Index.cs" />
<Compile Include="System\Linq\Intersect.cs" />
<Compile Include="System\Linq\Iterator.cs" />
<Compile Include="System\Linq\IIListProvider.cs" />
Expand Down
38 changes: 38 additions & 0 deletions src/libraries/System.Linq/src/System/Linq/Index.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace System.Linq
{
public static partial class Enumerable
{
/// <summary>Return index and the associated item.</summary>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an ideal description of what the method does: the method doesn't return an index and an associated item.

/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to return an element from.</param>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static IEnumerable<(int Index, TSource Item)> Index<TSource>(this IEnumerable<TSource> source)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eiriktsarpalis / @terrajobst, I see ~40 uses in source.dot.net of the Enumerable.Select overload that incorporates an index, 0 of which could switch to use this new method. Why do we believe it's valuable enough to ship?

{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}

return IndexIterator(source);
}

private static IEnumerable<(int Index, TSource Item)> IndexIterator<TSource>(IEnumerable<TSource> source)
{
int index = -1;
foreach (TSource element in source)
{
checked
{
index++;
}

yield return (index, element);
}
}
}
}
56 changes: 56 additions & 0 deletions src/libraries/System.Linq/tests/IndexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Xunit;

namespace System.Linq.Tests
{
public class IndexTests : EnumerableTests
{
[Fact]
public void Index_SourceIsNull_ArgumentNullExceptionThrown()
{
IEnumerable<int> source = null;

AssertExtensions.Throws<ArgumentNullException>("source", () => source.Index());
}

[ConditionalFact(typeof(TestEnvironment), nameof(TestEnvironment.IsStressModeEnabled))]
public void LargeEnumerable_ThrowsOverflowException()
{
long maxInt = int.MaxValue;
var overflowRange = RepeatedNumberGuaranteedNotCollectionType(num: 1, count: maxInt + 2).Index();
using (var en = overflowRange.GetEnumerator())
Assert.Throws<OverflowException>(() =>
{
while (en.MoveNext())
{
}
});
}

[ConditionalFact(typeof(TestEnvironment), nameof(TestEnvironment.IsStressModeEnabled))]
public void LargeEnumerable()
{
long maxInt = int.MaxValue;
int index = -1;
var range = RepeatedNumberGuaranteedNotCollectionType(num: 1, count: maxInt + 1).Index();
foreach (var item in range)
{
++index;
Assert.Equal(index, item.Index);
}
Assert.Equal(int.MaxValue, index);
}

[Fact]
public void Index()
{
string[] source = ["a", "b"];
Comment thread
RaymondHuy marked this conversation as resolved.
Resolved
(int Index, string Item)[] actual = source.Index().ToArray();
(int Index, string Item)[] expected = [(0, "a"), (1, "b")];
AssertExtensions.SequenceEqual<(int Index, string Item)>(expected, actual);
}
}
}
1 change: 1 addition & 0 deletions src/libraries/System.Linq/tests/System.Linq.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<Compile Include="FirstTests.cs" />
<Compile Include="GroupByTests.cs" />
<Compile Include="GroupJoinTests.cs" />
<Compile Include="IndexTests.cs" />
<Compile Include="IntersectTests.cs" />
<Compile Include="JoinTests.cs" />
<Compile Include="LastOrDefaultTests.cs" />
Expand Down