2017-04-25 88 views
1

我不知道爲什麼,但下面的代碼和LINQ語句返回0項,我不明白爲什麼。應該有3個重複條目....LINQ查詢返回重複列表不工作

List<SelectListItem> allClientUserAndCandidateViews = new List<SelectListItem>(); 
foreach (var clientUserView in clientUserViews) 
      { 
       SelectListItem item = 
        new SelectListItem 
        { 
         Value = clientUserView.ClientViewId.ToString(), 
         Text = clientUserView.Name       
        }; 
       allClientUserAndCandidateViews.Add(item); 
      } 

    List<SelectListItem> matchingClientUserAndCandidateViews = allClientUserAndCandidateViews 
      .GroupBy(x => x) 
      .Where(g => g.Count() > 1) 
      .Select(y => y.Key) 
      .ToList(); 

    List<SelectListItem> matchClientUserAndCandidateViews = allClientUserAndCandidateViews 
      .GroupBy(x => x)    
      .Where(g => g.Skip(1).Any()) 
      .SelectMany(g => g)   
      .ToList(); 
+1

你的意思是'GroupBy' x? Isnt x是「SelectedItem」類,因此組中不會有多個項目? –

回答

0

這樣做的原因行爲是SelectListItem使用的是默認實施GetHashCodeEqualssource),這意味着在(x => x)所有團體將會有確切的一個項目。

由於SelectListItem是你的代碼,解決問題外由值和文本,而不是分組:

List<SelectListItem> matchingClientUserAndCandidateViews = allClientUserAndCandidateViews 
     .GroupBy(x => new {x.Value, x.Text}) 
     .Where(g => g.Count() > 1) 
     .Select(y => y.First()) 
     .ToList(); 

List<SelectListItem> matchClientUserAndCandidateViews = allClientUserAndCandidateViews 
     .GroupBy(x => new {x.Value, x.Text})    
     .Where(g => g.Skip(1).Any()) 
     .SelectMany(g => g)   
     .ToList(); 
+0

請注意,'GroupBy'還具有[超過以IEqualityComparer ](https://msdn.microsoft.com/en-us/library/bb534334(v = vs.110).aspx)作爲參數 –

+0

@ZdeněkJelínekSure這是另一種可行的方法。然而,這個方法更加方便,因爲它不需要創建一個新的類。 – dasblinkenlight

+0

@dasblinkenlight非常感謝。我遇到了這樣一個非常困難的時期,並且查看了一百萬個SO帖子,提出了我使用的不正確的LINQ聲明。 LINQ混淆了我作爲初級開發者:( –

0

如果在你的代碼的SelectListItemthis one,那麼問題是關係到治療的所有元素爲不同因爲Distinct使用默認比較器,所以無法正確比較所有屬性。

this answer檢查一個完整的解決方案:

public class SelectListItemComparer : IEqualityComparer<SelectListItem> 
{ 
    public bool Equals(SelectListItem x, SelectListItem y) 
    { 
     return x.Text == y.Text && x.Value == y.Value; 
    } 

    public int GetHashCode(SelectListItem item) 
    { 
     int hashText = item.Text == null ? 0 : item.Text.GetHashCode(); 
     int hashValue = item.Value == null ? 0 : item.Value.GetHashCode(); 
     return hashText^hashValue; 
    } 
} 

作爲邊注,我想使應用程序邏輯(計算不同項目)和邏輯視圖(視圖模型如SelectListItem)之間的明確分離。特別是,您可以定義一些ItemServiceModel來保存相關數據,計算不同的項目並將它們映射到SelectListItem s。

0

在你GroupBy你需要GroupBy(x=> x.Value)GroupBy(x=>x.Text)

0

如果你正在尋找獲得分組和計數,這應該爲你工作。

var countByGroups = allClientUserAndCandidateViews.GroupBy(x => x.Text, (key, values) => new { key, Count = values.Count() }); 

您也可以使用LinqPad通過調用轉儲方法來查看您的分組的結果。複製將其粘貼到LinqPad中。

void Main() 
{ 
    var clientUserViews = new List<Test>(); 
    var t1 = new Test() 
    { 
     Text = "A", 
     Value = "A" 
    }; 
    var t2 = new Test() 
    { 
     Text = "A", 
     Value = "A" 
    }; 
    var t3 = new Test() 
    { 
     Text = "A", 
     Value = "A" 
    }; 

    clientUserViews.Add(t1); 
    clientUserViews.Add(t2); 
    clientUserViews.Add(t3); 
    List<Test> allClientUserAndCandidateViews = new List<Test>(); 
    foreach (var clientUserView in clientUserViews) 
    { 
     Test item = 
      new Test 
      { 
       Value = clientUserView.Value.ToString(), 
       Text = clientUserView.Text 
      }; 
     allClientUserAndCandidateViews.Add(item); 
    } 

    allClientUserAndCandidateViews.Dump("allClientUserAndCandidateViews"); 

    allClientUserAndCandidateViews.GroupBy(x => x.Text, (key, values) => new { key, Count = values.Count() }).Dump(); 

} 

// Define other methods and classes here 
public class Test 
{ 
    public string Value { get; set; } 
    public string Text { get; set; } 
}