2012-04-16 81 views
2

我有一個C#.Net網絡應用程序,我正在使用以下LINQ查詢獲取用戶創建的或用戶角色不同的建議清單。即使在Union和Distinct之後,要返回的列表也包含相同提案的模糊。我究竟做錯了什麼?LINQ Union and Distinct

 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(); 

回答

2

Proposal class是什麼定義?您可能對Proposal類的默認相等運算符有問題。 As msdn says about Distinct

通過使用默認的相等比較器對值進行比較返回序列不同元件。

編輯:換句話說,你有Equals和/或GetHashCode自定義實現?

+0

@sinelaw ....我們沒有自定義的Equality操作符。 HMTmm – MikeTWebb 2012-04-16 19:50:11

+0

MikeTWebb:沒有自定義的Equality操作符可能意味着比較是通過引用。你怎麼知道你有重複?順便說一句,你從哪裏得到'經理'枚舉?另一個問題可能是它首先包含重複項。 – sinelaw 2012-04-16 21:04:09

+0

@sinelaw .... thereturn是所有提案的列表,他們都有一個主鍵的Guid。我添加了上面的代碼。經濟將不會有重複。 – MikeTWebb 2012-04-16 21:33:13