| 1 |
/*-------------------------------------------------------------------------- |
| 2 |
* AnonymousComparer - lambda compare selector for Linq |
| 3 |
* ver 1.3.0.0 (Oct. 18th, 2010) |
| 4 |
* |
| 5 |
* created and maintained by neuecc <ils@neue.cc> |
| 6 |
* licensed under Microsoft Public License(Ms-PL) |
| 7 |
* http://neue.cc/ |
| 8 |
* http://linqcomparer.codeplex.com/ |
| 9 |
*--------------------------------------------------------------------------*/ |
| 10 |
|
| 11 |
using System.Collections.Generic; |
| 12 |
|
| 13 |
namespace System.Linq |
| 14 |
{ |
| 15 |
public static class AnonymousComparer |
| 16 |
{ |
| 17 |
#region IComparer<T> |
| 18 |
|
| 19 |
/// <summary>Example:AnonymousComparer.Create<int>((x, y) => y - x)</summary> |
| 20 |
public static IComparer<T> Create<T>(Func<T, T, int> compare) |
| 21 |
{ |
| 22 |
if (compare == null) throw new ArgumentNullException("compare"); |
| 23 |
|
| 24 |
return new Comparer<T>(compare); |
| 25 |
} |
| 26 |
|
| 27 |
private class Comparer<T> : IComparer<T> |
| 28 |
{ |
| 29 |
private readonly Func<T, T, int> compare; |
| 30 |
|
| 31 |
public Comparer(Func<T, T, int> compare) |
| 32 |
{ |
| 33 |
this.compare = compare; |
| 34 |
} |
| 35 |
|
| 36 |
public int Compare(T x, T y) |
| 37 |
{ |
| 38 |
return compare(x, y); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#endregion |
| 43 |
|
| 44 |
#region IEqualityComparer<T> |
| 45 |
|
| 46 |
/// <summary>Example:AnonymousComparer.Create((MyClass mc) => mc.MyProperty)</summary> |
| 47 |
public static IEqualityComparer<T> Create<T, TKey>(Func<T, TKey> compareKeySelector) |
| 48 |
{ |
| 49 |
if (compareKeySelector == null) throw new ArgumentNullException("compareKeySelector"); |
| 50 |
|
| 51 |
return new EqualityComparer<T>( |
| 52 |
(x, y) => |
| 53 |
{ |
| 54 |
if (object.ReferenceEquals(x, y)) return true; |
| 55 |
if (x == null || y == null) return false; |
| 56 |
return compareKeySelector(x).Equals(compareKeySelector(y)); |
| 57 |
}, |
| 58 |
obj => |
| 59 |
{ |
| 60 |
if (obj == null) return 0; |
| 61 |
return compareKeySelector(obj).GetHashCode(); |
| 62 |
}); |
| 63 |
} |
| 64 |
|
| 65 |
public static IEqualityComparer<T> Create<T>(Func<T, T, bool> equals, Func<T, int> getHashCode) |
| 66 |
{ |
| 67 |
if (equals == null) throw new ArgumentNullException("equals"); |
| 68 |
if (getHashCode == null) throw new ArgumentNullException("getHashCode"); |
| 69 |
|
| 70 |
return new EqualityComparer<T>(equals, getHashCode); |
| 71 |
} |
| 72 |
|
| 73 |
private class EqualityComparer<T> : IEqualityComparer<T> |
| 74 |
{ |
| 75 |
private readonly Func<T, T, bool> equals; |
| 76 |
private readonly Func<T, int> getHashCode; |
| 77 |
|
| 78 |
public EqualityComparer(Func<T, T, bool> equals, Func<T, int> getHashCode) |
| 79 |
{ |
| 80 |
this.equals = equals; |
| 81 |
this.getHashCode = getHashCode; |
| 82 |
} |
| 83 |
|
| 84 |
public bool Equals(T x, T y) |
| 85 |
{ |
| 86 |
return equals(x, y); |
| 87 |
} |
| 88 |
|
| 89 |
public int GetHashCode(T obj) |
| 90 |
{ |
| 91 |
return getHashCode(obj); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#endregion |
| 96 |
|
| 97 |
#region Extensions for LINQ Standard Query Operators |
| 98 |
|
| 99 |
// IComparer<T> |
| 100 |
|
| 101 |
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare) |
| 102 |
{ |
| 103 |
return source.OrderBy(keySelector, AnonymousComparer.Create(compare)); |
| 104 |
} |
| 105 |
|
| 106 |
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare) |
| 107 |
{ |
| 108 |
return source.OrderByDescending(keySelector, AnonymousComparer.Create(compare)); |
| 109 |
} |
| 110 |
|
| 111 |
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare) |
| 112 |
{ |
| 113 |
return source.ThenBy(keySelector, AnonymousComparer.Create(compare)); |
| 114 |
} |
| 115 |
|
| 116 |
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare) |
| 117 |
{ |
| 118 |
return source.ThenByDescending(keySelector, AnonymousComparer.Create(compare)); |
| 119 |
} |
| 120 |
|
| 121 |
// IEqualityComparer<T> |
| 122 |
|
| 123 |
public static bool Contains<TSource, TCompareKey>(this IEnumerable<TSource> source, TSource value, Func<TSource, TCompareKey> compareKeySelector) |
| 124 |
{ |
| 125 |
return source.Contains(value, AnonymousComparer.Create(compareKeySelector)); |
| 126 |
} |
| 127 |
|
| 128 |
public static IEnumerable<TSource> Distinct<TSource, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TCompareKey> compareKeySelector) |
| 129 |
{ |
| 130 |
return source.Distinct(AnonymousComparer.Create(compareKeySelector)); |
| 131 |
} |
| 132 |
|
| 133 |
public static IEnumerable<TSource> Except<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector) |
| 134 |
{ |
| 135 |
return first.Except(second, AnonymousComparer.Create(compareKeySelector)); |
| 136 |
} |
| 137 |
|
| 138 |
public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IEnumerable<TSource>, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 139 |
{ |
| 140 |
return source.GroupBy(keySelector, resultSelector, AnonymousComparer.Create(compareKeySelector)); |
| 141 |
} |
| 142 |
|
| 143 |
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 144 |
{ |
| 145 |
return source.GroupBy(keySelector, elementSelector, AnonymousComparer.Create(compareKeySelector)); |
| 146 |
} |
| 147 |
|
| 148 |
public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IEnumerable<TElement>, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 149 |
{ |
| 150 |
return source.GroupBy(keySelector, elementSelector, resultSelector, AnonymousComparer.Create(compareKeySelector)); |
| 151 |
} |
| 152 |
|
| 153 |
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult, TCompareKey>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 154 |
{ |
| 155 |
return outer.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, AnonymousComparer.Create(compareKeySelector)); |
| 156 |
} |
| 157 |
|
| 158 |
public static IEnumerable<TSource> Intersect<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector) |
| 159 |
{ |
| 160 |
return first.Intersect(second, AnonymousComparer.Create(compareKeySelector)); |
| 161 |
} |
| 162 |
|
| 163 |
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult, TCompareKey>(this IEnumerable<TOuter> outer, IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 164 |
{ |
| 165 |
return outer.Join(inner, outerKeySelector, innerKeySelector, resultSelector, AnonymousComparer.Create(compareKeySelector)); |
| 166 |
} |
| 167 |
|
| 168 |
public static bool SequenceEqual<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector) |
| 169 |
{ |
| 170 |
return first.SequenceEqual(second, AnonymousComparer.Create(compareKeySelector)); |
| 171 |
} |
| 172 |
|
| 173 |
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 174 |
{ |
| 175 |
return source.ToDictionary(keySelector, elementSelector, AnonymousComparer.Create(compareKeySelector)); |
| 176 |
} |
| 177 |
|
| 178 |
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement, TCompareKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, TCompareKey> compareKeySelector) |
| 179 |
{ |
| 180 |
return source.ToLookup(keySelector, elementSelector, AnonymousComparer.Create(compareKeySelector)); |
| 181 |
} |
| 182 |
|
| 183 |
public static IEnumerable<TSource> Union<TSource, TCompareKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TCompareKey> compareKeySelector) |
| 184 |
{ |
| 185 |
return first.Union(second, AnonymousComparer.Create(compareKeySelector)); |
| 186 |
} |
| 187 |
|
| 188 |
#endregion |
| 189 |
} |
| 190 |
} |