using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class LambdaComparer<T> : IEqualityComparer<T>
{
private readonly Func<T, T, bool> _lambdaComparer;
private readonly Func<T, int> _lambdaHash;
public LambdaComparer(Func<T, T, bool> lambdaComparer) :
this(lambdaComparer, o => o.GetHashCode())
{
}
public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash)
{
if (lambdaComparer == null)
throw new ArgumentNullException("lambdaComparer");
if (lambdaHash == null)
throw new ArgumentNullException("lambdaHash");
_lambdaComparer = lambdaComparer;
_lambdaHash = lambdaHash;
}
public bool Equals(T x, T y)
{
return _lambdaComparer(x, y);
}
public int GetHashCode(T obj)
{
return _lambdaHash(obj);
}
}
public static class IEnumerableExtension
{
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first,
IEnumerable<TSource> second , Func<TSource, TSource, bool> comparer )
{
return first.Except(second, new LambdaComparer<TSource>(comparer));
}
}
class Program
{
static void Main(string[] args)
{
List<DateTime>
firstlist = new List<DateTime>{
new DateTime(2009, 10, 5, 20, 15, 34),
new DateTime(2009, 10, 6, 20, 14, 34),
new DateTime(2009, 10, 7, 20, 16, 34),
new DateTime(2009, 10, 2, 20, 15, 34),
new DateTime(2009, 5, 5, 5, 5, 5)},
secondlist = new List<DateTime>{
new DateTime(2009, 11, 5, 20, 15, 34),
new DateTime(2009, 10, 6, 22, 14, 34),
new DateTime(2009, 11, 7, 20, 16, 34),
new DateTime(2009, 10, 2, 20, 15, 34),
new DateTime(2009, 5, 5, 5, 5, 5)},
thirdlist;
thirdlist = (from dt in firstlist.Except(secondlist,(a,b) => a.Date==b.Date) select dt.Date).ToList();
foreach (var d in thirdlist) Console.WriteLine(d);
}
}
}
Przepraszam, mam zły dzień. :-)