2

I want to use linq to object to remove duplicate records. I want that final list will contain unique records with the latest Date. I have the following list

AId             BId           C(date)
**1              2               24/5/2015**
3                6               24/5/2015
**1              2               23/5/2015** (Need To Remove)

I want to remove record 3.

Any Advise?

Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
avnic
  • 3,241
  • 8
  • 37
  • 46

4 Answers4

3

You can use GroupBy and then Select with OrderByDescending to order the dates.

public class Item 
{
    public int Aid {get;set;}

    public int Bid {get;set;}

    public DateTime CDate {get;set;}
}

void Main()
{
    var items = new List<Item>
    {
        new Item { Aid = 1, Bid = 2, CDate = new DateTime(2015, 5, 24)},
        new Item { Aid = 3, Bid = 6, CDate = new DateTime(2015, 5, 24)},
        new Item { Aid = 1, Bid = 2, CDate = new DateTime(2015, 5, 23)},        
    };

    var result =  items.GroupBy(i => i.Aid)
                  .Select(group => 
                        new { Key = group.Key,
                              Items = group.OrderByDescending(x => x.CDate) })
                  .Select (g => g.Items.First());


    result.Dump();
}
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
2

first find the duplicate values then delete them

  var DuplicateList = lst.GroupBy(x=>x)
              .Where(g=>g.Count()>1)
              .Select(y=>y)
              .ToList();

then loop on your list and delete one of each duplicate

for (int i = OriginalList.Count - 1; i >= 0; i--)
  {
    if(DuplicateList.Exists(x=>x.BId == OriginalList[i].BId)
        OriginalList.RemoveAt(i)
  }

Note you can use Any instead of Exists in the condition if your list is IEnumerable

so the condition will be

   if(DuplicateList.Any(x=>x.BId == OriginalList[i].BId)
            OriginalList.RemoveAt(i)
Hussein Khalil
  • 1,395
  • 11
  • 29
1

Group by Aid and Bid columns, and from each group select item with the latest C column date :

var result =  myTable.GroupBy(item => new {item.Aid, item.Bid})
                     .Select(group => group.OrderByDescending(item => item.C).First());
har07
  • 88,338
  • 12
  • 84
  • 137
0

use GroupBy:

var unique1 = table1.AsEnumerable()
                       .GroupBy(x=>x.Field<string>("AId"))
                       .Select(g=>g.First());

then if you loop through unique1 there will be one row with "AId" **1 Here we are specifically doing group with "AId because in your duplicate rows the dates are different

Sachu
  • 7,555
  • 7
  • 55
  • 94