Remove duplicate records using LINQ [duplicate]

十年热恋 提交于 2021-01-01 03:38:56

问题


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?


回答1:


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();
}



回答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)



回答3:


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());



回答4:


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



来源:https://stackoverflow.com/questions/30421153/remove-duplicate-records-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!