创建一个IQueryable列表是不同
问题描述:
我有对象,GroupStudentStatus的名单,我需要做出不同的。 我写了下面这个类来做到这一点。
的2个属性是相关是GroupStudentStatus.IsLastActionRemoved(日期时间)和GroupStudentStatus.Student.Guid。创建一个IQueryable列表是不同
protected List<GroupStudentStatus> RemovedStudents
{
get
{
return AllStudents.Where(s => s.IsLastActionRemoved).Distinct().OrderByDescending(d => d.LastActionDate).ToList();
}
}
public class GroupStudentStatusComparer : IEqualityComparer<GroupStudentStatus>
{
public GroupStudentStatus Compare(GroupStudentStatus x, GroupStudentStatus y)
{
//get the student that was last removed
if (!Equals(x, y))
{
return x.LastActionDate > y.LastActionDate ? x : y;
}
return x;
}
public bool Equals(GroupStudentStatus x, GroupStudentStatus y)
{
return x.Student.Guid.Equals(y.Student.Guid);
}
public int GetHashCode(GroupStudentStatus obj)
{
return obj.Student.Guid.GetHashCode();
}
}
我认为这是正确的,除了我不知道如何测试它。
我试图做到这一点:
return AllStudents.Where(s => s.IsLastActionRemoved)
.Distinct(new GroupStudentStatusComparer((x, y) => x.Compare(x,y)))
.OrderByDescending(d => d.LastActionDate).ToList();
答
return AllStudents.Where(s => s.IsLastActionRemoved)
.Distinct(new GroupStudentStatusComparer())
.OrderByDescending(d => d.LastActionDate)
.ToList();
答
return AllStudents.Where(s => s.IsLastActionRemoved).GroupBy(gss => gss.Student).Select(g => new GroupStudentStatus(g.Key, g.Select(gss2 => gss2.LastActionDate).Max(), true)).OrderByDescending(d => d.LastActionDate).ToList();
结束了使用GROUPBY。
'IEqualityComparer'不需要'比较'功能 – Magnus