我的表 StudentAccount 包含代码,金额,描述,有效期等列 对于选定的时间段,可能存在相同的代码和描述 写一个获取数据的linq查询代码/描述需要在所选日期范围内是唯一的。(日期格式为1/1 / 1990-1 / 1/1991)
public IEnumerable<StudentAccount> StudentAccountdata
{
get { return Context.StudentAccount.Where(q=>q.Active).OrderBy(q =>q.Description).ToList(); }
}
答案 0 :(得分:1)
像这样使用IEqualityComparer
:
public class StdComparer : IEqualityComparer<StudentAccount>
{
#region IEqualityComparer<StudentAccount> Members
public bool Equals(StudentAccount x, StudentAccount y)
{
return x.Code == y.Code && x.Description == y.Description;
}
public int GetHashCode(StudentAccount obj)
{
return 0;
}
#endregion
}
然后
public IEnumerable<StudentAccount> StudentAccountdata
{
get { return Context.StudentAccount.Where(q=>q.Active &&
q.Date >= BeginDate &&
q.Date <= EndDate)
.OrderBy(q =>q.Description).Distinct(new StdComparer()); }
}