【问题标题】:Distinct or GroupBy on a list which made of tuple<string,string,string>由 tuple<string,string,string> 组成的列表上的 Distinct 或 GroupBy
【发布时间】:2019-07-17 13:26:28
【问题描述】:

我有一个元组列表:List&lt;Tuple&lt;string,string,string&gt;&gt;

当我运行我的程序时,我的列表有这个:

List[0] : 'Cars','Bird','1.0';
List[1] : 'Plane','Flower','2.0';
List[2] : 'Cars','Bird','1.0';
List[3] : 'Plane','Flower','2.0';

我想在我的列表中的 linq 中创建一个组,使其只有两行:

List[0] : 'Cars','Bird','1.0';
List[1] : 'Plane','Flower','2.0';

它们是相同的,所以我不需要重复它。

我已经尝试了很多东西。我知道如何在列上进行分组。

但是对于 3 个字符串的元组,我不知道。

在我的测试中,我有 4 行。我只想要解释的两行。

【问题讨论】:

  • List&lt;Tuple&lt;string, string, string&gt;&gt; result = source.Distinct().ToList();
  • 我不明白这个问题。你试过什么? dotnetfiddle.net/BbN7Pj。 Distinct 和 GroupBy 都有效。你的尝试有什么错误?介意分享一个简单的minimal reproducible example简单。
  • 因为我们是同事(我曾在“中心变形专业人员”担任教职),这里是最好的建议。 1/;阅读How to Askminimal reproducible example,这是我在日常职业生涯中仍然使用的有用工具。您甚至可以为学生翻译它们。清晰和精确并提供演示代码而不是整个解决方案将是他们的改进。 2/;将您的名字换成别名 - 学生找到我们时可能会表现得很奇怪。

标签: c# .net list linq


【解决方案1】:

如果你真的有List&lt;Tuple&lt;string, string, string&gt;&gt;,你可以只做myList.Distinct()

var myList = new List<Tuple<string, string, string>> {
 Tuple.Create("a", "b", "1"),
 Tuple.Create("aa", "bb", "1"),
 Tuple.Create("a", "b", "1"), 
 Tuple.Create("aa", "bb", "1")};

myList.Distinct().Dump();   

【讨论】:

    【解决方案2】:

    如果你想按所有列分组,那么你可以只使用 Distinct 扩展方法。

    list.Distinct();
    

    如果要按单列分组:

        List<Tuple<string, string, string>> list = new List<Tuple<string, string, string>>();
        list.Add(Tuple.Create("Cars", "Bird", "1.0"));
        list.Add(Tuple.Create("Plane", "Flower", "1.0"));
        list.Add(Tuple.Create("Cars", "Bird", "2.0"));
        list.Add(Tuple.Create("Plane", "Flower", "2.0"));
    
        foreach (var group in list.GroupBy(tuple => tuple.Item1))
        {
            Console.WriteLine(group.Key);
    
            foreach (var tuple in group)
            {
                Console.WriteLine("  " + tuple);
            }
        }
    

    结果是:

    Cars
      (Cars, Bird, 1.0)
      (Cars, Bird, 2.0)
    Plane
      (Plane, Flower, 1.0)
      (Plane, Flower, 2.0)
    

    【讨论】:

      【解决方案3】:

      您可以按 Item1、Item2 和 Item3 分组

      var myList = new List<Tuple<string, string, string>> {
       Tuple.Create("a", "b", "1"),
       Tuple.Create("aa", "bb", "1"),
       Tuple.Create("a", "b", "1"), 
       Tuple.Create("aa", "bb", "1")};
      
      var result = myList.GroupBy(x => new { x.Item1, x.Item2,x.Item3})
      

      【讨论】:

        【解决方案4】:

        您实际上不需要GroupBy 函数,而是Distinct 函数。

        元组支持相等检查,因此为了删除所有重复项,您只需编写:

        var listWithoutDuplicates = myList.Distinct().ToList();
        


        如果它不是实现IEquatable<T> 泛型接口的元组或其他类型,您可能希望使用DistinctBy 函数,因为它没有本机.NET 实现,您可以阅读更多关于here .

        【讨论】:

        • var myList = myList.Distinct().ToList(); 如果我们想得到List&lt;&gt; 作为结果
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 2011-07-06
        相关资源
        最近更新 更多