Skip to content

Instantly share code, notes, and snippets.

@toburger
Created October 1, 2012 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toburger/3810474 to your computer and use it in GitHub Desktop.
Save toburger/3810474 to your computer and use it in GitHub Desktop.
Enumerable Extension Method to Distinct or Union IEnumerable(s) by a property without the need to create an IEqualityComparer<T> implementation.
static class EnumerableExtensions
{
public static IEnumerable<T> Distinct<T, TProperty>(
this IEnumerable<T> source,
Expression<Func<T, TProperty>> propertyExpression)
{
return source.Distinct(
new PropertyExpressionComparer<T, TProperty>(propertyExpression));
}
public static IEnumerable<T> Union<T, TProperty>(
this IEnumerable<T> first,
IEnumerable<T> second,
Expression<Func<T, TProperty>> propertyExpression)
{
return first.Union(
second,
new PropertyExpressionComparer<T, TProperty>(propertyExpression));
}
private class PropertyExpressionComparer<T, TProperty> : IEqualityComparer<T>
{
private readonly PropertyInfo _propertyInfo;
public PropertyExpressionComparer(Expression<Func<T, TProperty>> propertyExpression)
{
string propertyName = ((MemberExpression)propertyExpression.Body).Member.Name;
_propertyInfo = typeof(T).GetProperty(propertyName);
}
public bool Equals(T x, T y)
{
return _propertyInfo.GetValue(x).Equals(_propertyInfo.GetValue(y));
}
public int GetHashCode(T obj)
{
return _propertyInfo.GetValue(obj).GetHashCode();
}
}
}
@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage:

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage:

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage:

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage:

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage:

@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage:

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

example usage

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

usage:

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

@toburger
Copy link
Author

toburger commented Oct 1, 2012

var res = from p in Process.GetProcesses()
          orderby p.ProcessName
          select p;

foreach (var item in res.Distinct(p => p.ProcessName))
{
    Console.WriteLine(item.ProcessName);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment