Husain Alhamali
Husain Alhamali

Reputation: 823

C# remove datatable duplicates based on field and condition

Is it possible to remove duplicates from a datatable based on a specific field and a condition

Like if I have the following records:

name: Ali

appointment_type: dental

appointment_date: 8/5/2017 08:00:00

name: ali

appointment_type: dental

appointment_date: 8/5/2017 16:00:00

from the above example, the patient Ali has two appointments, I want to remove the later appointment(s) (which is on 8/5/2017 16:00:00)

in other words, remove all appointment of patient "Ali" and keep the earliest one only

is it possible to do it in LINQ?

Upvotes: 1

Views: 96

Answers (2)

jdweng
jdweng

Reputation: 34433

Try following :

        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("appointment_type", typeof(string));
            dt.Columns.Add("appointment_date", typeof(DateTime));

            dt.Rows.Add(new object[] { "Ali", "dental", DateTime.Parse("8/5/2017 08:00:00")});
            dt.Rows.Add(new object[] { "Ali", "dental", DateTime.Parse("8/5/2017 16:00:00")});

            var groups = dt.AsEnumerable().GroupBy(x => new { name = x.Field<string>("name"), type = x.Field<string>("appointment_type") }).ToList();

            dt = groups.Select(x => x.OrderBy(y => y.Field<DateTime>("appointment_date")).LastOrDefault()).CopyToDataTable();

        }

Upvotes: 0

Ian
Ian

Reputation: 30823

You might want to GroupBy the items and then OrderBy each group based on AppointmentDate, take only the First (earliest) from each group. The result will be the earliest appointment only:

List<Patient> patients = new List<Patient>(); //change this with your actual list/IEnumerable

IEnumerable<Patient> earliestAppointmentRecorded = patients.GroupBy(x => x.Name.ToLower().Trim())
   .Select(x => x.OrderBy(y => y.AppointmentDate).First());

Assuming the class is like below:

public class Patient {
  public string Name { get; set; }
  public string AppointmentType { get; set; }
  public DateTime AppointmentDate { get; set; }

};

And, say, you want to replace the earlier records with the ones of the earliestAppointmentRecorded, you could simply do:

patients = earliestAppointmentRecorded.ToList();

Upvotes: 1

Related Questions