如果你有一个辅助类来创建一个比较器(如下图所示),你可以做这样的:
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