How to remove repetitive lines



  • The table is as follows:

    price|text
    2    |one
    3    |one
    6    |two
    4    |two
    

    How do I find the duplicate lines in the column? text and at the same time compare them with the price column and remove them with the highest price?

    My code to remove the duplicate cell. text:

     foreach (DataRow row in price_table.Rows
                                   .Cast<DataRow>()
                                   .GroupBy(row => row["text"])
                                   .Where(g => g.Count() > 1)   
                                   .SelectMany(g => g.Skip(1))
                                  )
            {
            row.Delete();
    
        }
    price_table.AcceptChanges();
    

    Thank you, it's a bit of a fix.

    foreach (DataRow row in source_price.Rows
    .Cast<DataRow>()
    .GroupBy(row => row["text"])
    .Where(g => g.Count() > 1)

                          .Select(g =&gt; g.OrderBy(r =&gt; r.Field&lt;string&gt;("Price")).Last())
                          )
    



  • Group the text, separate each price group and select one element from the group:

    foreach (DataRow row in price_table.Rows
                                  .Cast<DataRow>()
                                  .GroupBy(row => row.Field<string>("text"))
                                  .Select(g => g.OrderBy(r => r.Field<int>("Price")).First()))
    {
        row.Delete();
    

    }
    price_table.AcceptChanges();


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2