LINQ查询返回列表中的重复不起作用

时间:2017-04-25 17:49:58

标签: c# linq

我不确定为什么但是下面的代码和LINQ语句返回0项,我无法弄清楚原因。应该有3个重复的条目....

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

4 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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

答案 2 :(得分:0)

在您的GroupBy中,您需要GroupBy(x=> x.Value)GroupBy(x=>x.Text)

答案 3 :(得分: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; } 
}