我有一个C#.Net网络应用程序,我使用以下LINQ查询来获取用户创建或具有各种用户角色的不同提案列表。即使在Union和Distinct之后,要返回的List也包含相同提案的dupes。我做错了什么?
var thereturn = FindAll(DetachedCriteria.For<Proposal>(),
new Order("CreateDate", false));
//get the proposals that aUser created
IList<Proposal> it =
thereturn.Where(proposal => proposal.CreatedBy.Equals(aUser)).ToList();
//get the proposals that aUser is a BOE Author
IList<Proposal> it2 =
thereturn.Where(proposal =>
proposal.BOEs.Any(boe =>
boe.Users.Where(a => a.Name == aUser).Any())).ToList();
//get all other proposals that aUser is on
IList<Proposal> it3 =
thereturn.Where(proposal =>
proposal.Users.Where(o => o.Name == aUser).Any()).ToList();
//now union with all other proposals that aUser is on
return it3.Union(it).Union(it2).
OrderByDescending(o=>o.CreateDate).Distinct().ToList();
答案 0 :(得分:2)
Proposal
类的定义是什么?对于Proposal
类,您的默认相等运算符可能存在问题。 As msdn says about Distinct:
通过使用默认的相等比较器来比较值,从序列返回不同的元素。
编辑:换句话说,您是否有Equals
和/或GetHashCode
的自定义实施?