-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (69 loc) · 2.62 KB
/
Program.cs
File metadata and controls
83 lines (69 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DistinctSample
{
class MovieActor
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string CharacterName { get; set; }
public override string ToString()
{
return String.Format("{0} \"{1}\" {2}", FirstName, CharacterName, LastName);
}
public static List<MovieActor> CreateSome()
{
return new List<MovieActor>()
{
new MovieActor() { FirstName = "Brad", LastName = "Pitt", CharacterName = "Rusty"},
new MovieActor() { FirstName = "Andy", LastName = "Garcia", CharacterName = "Terry"},
new MovieActor() { FirstName = "George", LastName = "Clooney", CharacterName = "Dany"},
new MovieActor() { FirstName = "Julia", LastName = "Roberts", CharacterName = "Tess"},
new MovieActor() { FirstName = "Julia", LastName = "Roberts", CharacterName = "Julia Roberts"}
};
}
}
class ByKeyEqualityComparer<T> : IEqualityComparer<T>
{
public Func<T, object> KeySelector { get; set; }
public bool Equals(T x, T y)
{
return KeySelector(x).Equals(KeySelector(y));
}
public int GetHashCode(T obj)
{
return KeySelector(obj).GetHashCode();
}
public ByKeyEqualityComparer(Func<T, object> keySelector)
{
KeySelector = keySelector;
}
}
class Program
{
static void Main(string[] args)
{
var actors = MovieActor.CreateSome();
actors.Add(new MovieActor() { FirstName = "George", LastName = "Clooney", CharacterName = "Dany"});
Console.WriteLine(String.Format("{0} actors total.", actors.Count()));
var distinct = actors.Distinct(new ByKeyEqualityComparer<MovieActor>(a => new { a.LastName, a.FirstName, a.CharacterName }));
foreach (var actor in distinct)
{
Console.WriteLine(actor);
}
Console.WriteLine("\nGroup trick:");
foreach (var actor in DistinctWithGroup(actors))
{
Console.WriteLine(actor);
}
Console.ReadLine();
}
static IEnumerable<MovieActor> DistinctWithGroup(IEnumerable<MovieActor> actors)
{
return actors.GroupBy(a => new { a.LastName, a.FirstName, a.CharacterName }).Select(g => g.First());
}
}
}