2017-04-25 95 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; } 
}