如果你有一個輔助類來創建一個比較器(如下圖所示),你可以做這樣的:
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 });
這裏是輔助類
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);
}
}
它運行完整的源代碼測試LinqPad下是在這裏:https://gist.github.com/hoganlong/5820080
哪個返回如下:
注:我建議在LinqPad.com LinqPad解決這些類型的問題 - 它的岩石。
感謝您的回答!並及時回覆! – Mazher