Advertisement
uniblab

Except`4

Feb 6th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System.Linq;
  2.  
  3. public static class Enumerable {
  4.  
  5.     public static System.Collections.Generic.IEnumerable<TOuter> Except<TOuter, TInner, TKey, TResult>(
  6.         this System.Collections.Generic.IEnumerable<TOuter> outer,
  7.         System.Collections.Generic.IEnumerable<TInner> inner,
  8.         System.Func<TOuter, TKey> outerKeySelecter,
  9.         System.Func<TInner, TKey> innerKeySelecter,
  10.         System.Collections.Generic.IEqualityComparer<TKey> comparer
  11.     ) {
  12.         var i = inner.ToDictionary( innerKeySelecter, comparer );
  13.         return outer.Where(
  14.             x => !i.ContainsKey( outerKeySelecter( x ) )
  15.         );
  16.     }
  17.  
  18.     public static System.Collections.Generic.IDictionary<K, V> ToDictionary<K, V>(
  19.         this System.Collections.Generic.IEnumerable<V> collection,
  20.         System.Func<V, K> keySelector,
  21.         System.Collections.Generic.IEqualityComparer<K> keyComparer
  22.     ) {
  23.         System.Collections.Generic.IDictionary<K, V> output = new System.Collections.Generic.Dictionary<K, V>( keyComparer );
  24.         foreach ( var item in collection ) {
  25.             output.Add( keySelector( item ), item );
  26.         }
  27.         return output;
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement