|
3 | 3 |
|
4 | 4 | using System.Collections; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.ComponentModel; |
6 | 7 | using System.Diagnostics.CodeAnalysis; |
7 | 8 | using System.Linq.Expressions; |
| 9 | +using System.Runtime.CompilerServices; |
8 | 10 |
|
9 | 11 | namespace System.Linq |
10 | 12 | { |
@@ -1775,6 +1777,33 @@ public static long LongCount<TSource>(this IQueryable<TSource> source, Expressio |
1775 | 1777 | Expression.Quote(keySelector))); |
1776 | 1778 | } |
1777 | 1779 |
|
| 1780 | + /// <summary>Returns the minimum value in a generic <see cref="IQueryable{T}"/> according to a specified key selector function.</summary> |
| 1781 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 1782 | + /// <typeparam name="TKey">The type of key to compare elements by.</typeparam> |
| 1783 | + /// <param name="source">A sequence of values to determine the minimum value of.</param> |
| 1784 | + /// <param name="keySelector">A function to extract the key for each element.</param> |
| 1785 | + /// <param name="comparer">The <see cref="IComparer{TSource}" /> to compare elements.</param> |
| 1786 | + /// <returns>The value with the minimum key in the sequence.</returns> |
| 1787 | + /// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception> |
| 1788 | + /// <exception cref="ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="IComparable" /> or <see cref="IComparable{TSource}" /> interface.</exception> |
| 1789 | + [DynamicDependency("MinBy`2", typeof(Enumerable))] |
| 1790 | + [Obsolete(Obsoletions.QueryableMinByMaxByTSourceObsoleteMessage, DiagnosticId=Obsoletions.QueryableMinByMaxByTSourceObsoleteDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] |
| 1791 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 1792 | + [OverloadResolutionPriority(-1)] |
| 1793 | + public static TSource? MinBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TSource>? comparer) |
| 1794 | + { |
| 1795 | + ArgumentNullException.ThrowIfNull(source); |
| 1796 | + ArgumentNullException.ThrowIfNull(keySelector); |
| 1797 | + |
| 1798 | + return source.Provider.Execute<TSource>( |
| 1799 | + Expression.Call( |
| 1800 | + null, |
| 1801 | + new Func<IQueryable<TSource>, Expression<Func<TSource, TKey>>, IComparer<TSource>, TSource?>(MinBy).Method, |
| 1802 | + source.Expression, |
| 1803 | + Expression.Quote(keySelector), |
| 1804 | + Expression.Constant(comparer, typeof(IComparer<TSource>)))); |
| 1805 | + } |
| 1806 | + |
1778 | 1807 | /// <summary>Returns the minimum value in a generic <see cref="IQueryable{T}"/> according to a specified key selector function.</summary> |
1779 | 1808 | /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
1780 | 1809 | /// <typeparam name="TKey">The type of key to compare elements by.</typeparam> |
@@ -1865,6 +1894,33 @@ public static long LongCount<TSource>(this IQueryable<TSource> source, Expressio |
1865 | 1894 | Expression.Quote(keySelector))); |
1866 | 1895 | } |
1867 | 1896 |
|
| 1897 | + /// <summary>Returns the maximum value in a generic <see cref="IQueryable{T}"/> according to a specified key selector function.</summary> |
| 1898 | + /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
| 1899 | + /// <typeparam name="TKey">The type of key to compare elements by.</typeparam> |
| 1900 | + /// <param name="source">A sequence of values to determine the maximum value of.</param> |
| 1901 | + /// <param name="keySelector">A function to extract the key for each element.</param> |
| 1902 | + /// <param name="comparer">The <see cref="IComparer{TSource}" /> to compare elements.</param> |
| 1903 | + /// <returns>The value with the maximum key in the sequence.</returns> |
| 1904 | + /// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception> |
| 1905 | + /// <exception cref="ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="IComparable" /> or <see cref="IComparable{TSource}" /> interface.</exception> |
| 1906 | + [DynamicDependency("MaxBy`2", typeof(Enumerable))] |
| 1907 | + [Obsolete(Obsoletions.QueryableMinByMaxByTSourceObsoleteMessage, DiagnosticId=Obsoletions.QueryableMinByMaxByTSourceObsoleteDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] |
| 1908 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 1909 | + [OverloadResolutionPriority(-1)] |
| 1910 | + public static TSource? MaxBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TSource>? comparer) |
| 1911 | + { |
| 1912 | + ArgumentNullException.ThrowIfNull(source); |
| 1913 | + ArgumentNullException.ThrowIfNull(keySelector); |
| 1914 | + |
| 1915 | + return source.Provider.Execute<TSource>( |
| 1916 | + Expression.Call( |
| 1917 | + null, |
| 1918 | + new Func<IQueryable<TSource>, Expression<Func<TSource, TKey>>, IComparer<TSource>, TSource?>(MaxBy).Method, |
| 1919 | + source.Expression, |
| 1920 | + Expression.Quote(keySelector), |
| 1921 | + Expression.Constant(comparer, typeof(IComparer<TSource>)))); |
| 1922 | + } |
| 1923 | + |
1868 | 1924 | /// <summary>Returns the maximum value in a generic <see cref="IQueryable{T}"/> according to a specified key selector function.</summary> |
1869 | 1925 | /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> |
1870 | 1926 | /// <typeparam name="TKey">The type of key to compare elements by.</typeparam> |
|
0 commit comments