Last active
December 18, 2015 17:39
-
-
Save hoganlong/5820080 to your computer and use it in GitHub Desktop.
For stackoverflow question at http://stackoverflow.com/questions/17204123/linq-retrieve-non-repeated-values#17204179
To run use linqpad and set it to C# program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
List<Event_Day> Days = new List<Event_Day>(); | |
Days.Add(new Event_Day(420,120,1,new DateTime(2013,6,20))); | |
Days.Add(new Event_Day(421,120,2,new DateTime(2013,6,21))); | |
Days.Add(new Event_Day(422,120,3,new DateTime(2013,6,22))); | |
List<Event_Session> Sessions = new List<Event_Session>(); | |
Sessions.Add(new Event_Session(170,120,420)); | |
Sessions.Add(new Event_Session(171,120,420)); | |
Sessions.Add(new Event_Session(172,120,420)); | |
Sessions.Add(new Event_Session(173,120,421)); | |
Sessions.Add(new Event_Session(174,120,421)); | |
Sessions.Add(new Event_Session(175,120,421)); | |
var unique_session = Sessions.Distinct( | |
new GenComp<Event_Session>((a,b) => (a.Event_Id == b.Event_Id) && (a.Event_Day_Id == b.Event_Day_Id) , (a) => a.Event_Id.GetHashCode()+a.Event_Day_Id.GetHashCode())); | |
var result = unique_session.Join(Days, | |
s => new { Event_Id = s.Event_Id, Event_Day_Id = s.Event_Day_Id }, | |
d => new { Event_Id = d.Event_Id, Event_Day_Id = d.Event_Day_Id }, | |
(s, d) => new { Event_Day_Id = d.Event_Day_Id, DayNo = d.DayNo, DayDate = d.Day_Date }); | |
result.Dump(); | |
} | |
public class GenComp<T> : IEqualityComparer<T> | |
{ | |
public Func<T, T, bool> comp { get; private set; } | |
public Func<T, int> hash { get; private set; } | |
public GenComp(Func<T, T, bool> inComp, Func<T,int> inHash) | |
{ | |
comp = inComp; | |
hash = inHash; | |
} | |
public GenComp(Func<T, T, bool> inComp) | |
{ | |
comp = inComp; | |
hash = null; | |
} | |
public bool Equals(T x, T y) | |
{ | |
return comp(x, y); | |
} | |
public int GetHashCode(T obj) | |
{ | |
return hash == null ? obj.GetHashCode() : hash(obj); | |
} | |
} | |
public class Event_Day | |
{ | |
public int Event_Day_Id { get; set; } | |
public int Event_Id { get; set; } | |
public int DayNo { get; set; } | |
public DateTime Day_Date { get; set; } | |
public Event_Day(int event_day_id, int event_id, int dayno, DateTime day_date) | |
{ | |
Event_Day_Id = event_day_id; | |
Event_Id = event_id; | |
DayNo = dayno; | |
Day_Date = day_date; | |
} | |
} | |
public class Event_Session | |
{ | |
public int Event_Session_Id { get; set; } | |
public int Event_Id { get; set; } | |
public int Event_Day_Id { get; set; } | |
public Event_Session(int event_session_id, int event_id, int event_day_id) | |
{ | |
Event_Session_Id = event_session_id; | |
Event_Id = event_id; | |
Event_Day_Id = event_day_id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment