【问题标题】:DataTable: how to get the duplicates and the row number of the duplicatesDataTable:如何获取重复项和重复项的行号
【发布时间】:2017-09-15 12:49:29
【问题描述】:

我有以下数据表:

Article  Price
ART1        99
ART2       100   
ART3       150 
ART2        90
ART1        50

现在,我应该创建一个新的数据表,其中包含重复项的位置

Article  Duplicates
ART1            1,5
ART2            2,4        
ART3          
ART2            2,4
ART1            1,5

所以关键是“文章”栏

我只发现了有关查找哪些是重复值以及使用 linq 重复值的次数的示例。

我如何使用 linq 实现类似的功能?

谢谢

【问题讨论】:

    标签: c# linq datatable


    【解决方案1】:

    您可以使用这种方法:

    var articleLookup = yourTable.AsEnumerable()
        .Select((row, index) => new { Row = row, RowNum = index + 1 })
        .ToLookup(x=> x.Row.Field<string>("Article"));
    
    DataTable dupTable = new DataTable();
    dupTable.Columns.Add("Article");
    dupTable.Columns.Add("Duplicates");
    
    foreach(DataRow row in yourTable.Rows)
    {
        DataRow addedRow = dupTable.Rows.Add();
        string article = row.Field<string>("Article");
        var dupRowNumList = articleLookup[article].Select(x => x.RowNum).ToList();
        string dupRowNumText = dupRowNumList.Count == 1 ? "" : String.Join(",", dupRowNumList);
        addedRow.SetField("Article", article);
        addedRow.SetField("Duplicates", dupRowNumText);
    }
    

    【讨论】:

      【解决方案2】:

      您好,我通过创建列表对象尝试了您的确切要求。我可以得到您需要的预期结果。重要的是你有 Linq 查询,它会给你结果。 这是主类

      class Program
      {
          static void Main(string[] args)
          {
      
              List<data> datas = new List<data>();
      
              datas.Add(new data() {atricle = "ART1", price = 99});
              datas.Add(new data() { atricle = "ART2", price = 100 });
              datas.Add(new data() { atricle = "ART3", price = 150 });
              datas.Add(new data() { atricle = "ART2", price = 90 });
              datas.Add(new data() { atricle = "ART1", price = 50 });
      
              Console.WriteLine($"Atricle | Duplicates");
              foreach (data templist in datas)
              {
                  var duplicates = datas.Select((data, index) => new {atricle = data.atricle, Index = index + 1})
                      .Where(x => x.atricle == templist.atricle)
                      .GroupBy(pair => pair.atricle)
                      .Where(g => g.Count() > 1)
                      .Select(grp => grp.Select(g => g.Index.ToString()).ToArray())
                      .ToArray();
                  string joined = duplicates.Length>0 ? string.Join(",", duplicates[0].ToList()):"";
                  Console.WriteLine($"{templist.atricle} | {joined}");               
              }
      
      
              Console.ReadLine();
      
          }
      }
      

      这里是数据模型类

      public class data{
      public string atricle { get; set; }
          public int price { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-31
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多