http://stackoverflow.com/questions/3446442/remove-duplicate-based-on-column-value-linq
https://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx
List<Employee> employees = new List<Employee>()
{
new Employee{Id =1,Name="AAAAA"}
, new Employee{Id =2,Name="BBBBB"}
, new Employee{Id =3,Name="AAAAA"}
, new Employee{Id =4,Name="CCCCC"}
, new Employee{Id =5,Name="AAAAA"}
};
List<Employee> duplicateEmployees = employees.Except(employees.GroupBy(i => i.Name)
.Select(ss => ss.FirstOrDefault()))
.ToList();
https://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx
Product[] products = { new Product { Name = "apple", Code = 9 },
new Product { Name = "orange", Code = 4 },
new Product { Name = "apple", Code = 9 },
new Product { Name = "lemon", Code = 12 } };
//Exclude duplicates.
IEnumerable<Product> noduplicates =
products.Distinct();
foreach (var product in noduplicates)
Console.WriteLine(product.Name + " " + product.Code);
/*
This code produces the following output:
apple 9
orange 4
lemon 12
*/
http://stackoverflow.com/questions/30421153/remove-duplicate-records-using-linq
You can useGroupByand thenSelectwithOrderByDescendingto 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(); }
http://stackoverflow.com/questions/3446442/remove-duplicate-based-on-column-value-linq
Okay, if you don't care which employee is removed, you could try something like:var result = query.GroupBy(x => x.EmployeeId) .Select(group => group.First());You haven't specified whether this is in LINQ to SQL, LINQ to Objects or something else... I don't know what the SQL translation of this would be. If you're dealing with a relatively small amount of data you could always force this last bit to be in-process:var result = query.AsEnumerable() .GroupBy(x => x.EmployeeId) .Select(group => group.First());At that point you could actually use MoreLINQ which has a handyDistinctBymethod:var result = query.AsEnumerable() .DistinctBy(x => x.EmployeeId);
No comments:
Post a Comment