Remove Duplicates in the List Using Linq

Remove duplicates in the list using linq

var distinctItems = items.Distinct();

To match on only some of the properties, create a custom equality comparer, e.g.:

class DistinctItemComparer : IEqualityComparer<Item> {

public bool Equals(Item x, Item y) {
return x.Id == y.Id &&
x.Name == y.Name &&
x.Code == y.Code &&
x.Price == y.Price;
}

public int GetHashCode(Item obj) {
return obj.Id.GetHashCode() ^
obj.Name.GetHashCode() ^
obj.Code.GetHashCode() ^
obj.Price.GetHashCode();
}
}

Then use it like this:

var distinctItems = items.Distinct(new DistinctItemComparer());

remove duplication from list on field using linq

Since the ID is unique you could use this approach (Dept seems to be a string):

var empDupNoDepartment = empList
.GroupBy(x => String.IsNullOrEmpty(x.Dept) ? int.MinValue : x.ID)
.Select(group => group.First())
.ToList();

This keeps only the first employee with empty Dept.

How to remove (via linq) duplicates from a List of objects

Not exactly an answer to your question (the other answers are all valid solutions for that), but if for some reason you're looking to actually extract your duplicate objects, such as for debugging, error processing, whatever, I wanted to offer the below.

var duplicates = someList
.GroupBy(r => r.Id)
.Where(g => g.Count() > 1)
.ToList();

Then you have a slightly different way to manage your list from pure distinct

someList = someList.Except(duplicates).ToList();

Which is then a list of keys which had no duplicates.

removing duplicates in a list with linq

You can use GroupBy() and select the first item of each group to achieve what you want - assuming you want to pick one item for each distinct ObjectId property:

var distinctList = myList.GroupBy(x => x.ObjectID)
.Select(g => g.First())
.ToList();

Alternatively there is also DistinctBy() in the MoreLinq project that would allow for a more concise syntax (but would add a dependency to your project):

var distinctList = myList.DistinctBy( x => x.ObjectID).ToList();

Filter List using linq for remove duplicates items

You need to project the grouping rows for each group to select the record with the highest date using OrderByDescensing with Select after GroupBy like :

var x = ListFamily.GroupBy(p => p.dni)
.Select(g=>g.OrderByDescending(x=>x.Adate)
.FirstOrDefault()
);

Remove duplicate records using LINQ

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

Take max date row from duplicates and remove duplicates in the list using linq

If suppose that new EmployeeSalaryFactory().GetRelatedObjects(...) returns list of EmployeeSalary objects:

List<EmployeeSalary> lstEmployeeSalary = 
new EmployeeSalaryFactory().GetRelatedObjects(...)
.GroupBy(x => x.Id)
.Select(g => g.OrderByDescending(o => o.DateOfSalary).First());

Test

IList<EmployeeSalary> clients = new List<EmployeeSalary>()
{
new EmployeeSalary { Id=1, Name = "Item1", EmpCode="IT00001", Salary=100, DateOfSalary= new DateTime(2021,5,26)},
new EmployeeSalary { Id=2, Name = "Item2", EmpCode="IT00002", Salary=200, DateOfSalary= new DateTime(2021,4,26)},
new EmployeeSalary { Id=3, Name = "Item3", EmpCode="IT00003", Salary=150, DateOfSalary= new DateTime(2021,5,26)},
new EmployeeSalary { Id=1, Name = "Item1", EmpCode="IT00001", Salary=100, DateOfSalary= new DateTime(2021,4,26)},
new EmployeeSalary { Id=3, Name = "Item3", EmpCode="IT00003", Salary=150, DateOfSalary= new DateTime(2021,4,26)},
}

var res = clients.GroupBy(x => new { x.Id, x.Name, x.EmpCode, x.Salary })
.Select(g => g.OrderByDescending(o => o.DateOfSalary).First());

foreach (var it in res.ToList())
{
System.Diagnostics.Debug.WriteLine(it.Id + ", " + it.Name + ", " + it.EmpCode + ", " + it.Salary + ", " + it.DateOfSalary);
}

OUTPUT:

1, Item1, IT00001, 100, 5/26/2021 00:00:00
2, Item2, IT00002, 200, 4/26/2021 00:00:00
3, Item3, IT00003, 150, 5/26/2021 00:00:00

Remove duplicate strings in list using C# / LinQ but ignore case

try this

  var result = testList
.GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
.Where(g => g.Count() == 1)
.Select(g => g.Key)
.ToList();

How to remove duplicates from a List of List using Linq

Your checking of existences of ID is invalid, as you are checking the first char of the ID of the row for equality with the string ID of the new line. (That's why to had also needed to use ToString). You can simplify your logic like:

private static bool DuplicateItem(List<List<string>> basket, string[] line)
{
return basket.Exists(row => row[0] == line[0]);
}


Related Topics



Leave a reply



Submit