C# LINQ find duplicates in List
The easiest way to solve the problem is to group the elements based on their value, and then pick a representative of the group if there are more than one element in the group. In LINQ, this translates to:
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
If you want to know how many times the elements are repeated, you can use:
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();
This will return a List
of an anonymous type, and each element will have the properties Element
and Counter
, to retrieve the information you need.
And lastly, if it's a dictionary you are looking for, you can use
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());
This will return a dictionary, with your element as key, and the number of times it's repeated as value.
How to get duplicate items from a list using LINQ?
var duplicates = lst.GroupBy(s => s)
.SelectMany(grp => grp.Skip(1));
Note that this will return all duplicates, so if you only want to know which items are duplicated in the source list, you could apply Distinct
to the resulting sequence or use the solution given by Mark Byers.
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());
How to generate duplicate items in a list using LINQ?
You can use Enumerable.SelectMany
+ Enumerable.Range
:
var result = inventoryDb.Pricing.AsNoTracking()
.Where(p => p.Quantity > 0m)
.SelectMany(p => Enumerable.Range(0, p.Quantity)
.Select(i => new
{
TagNo = p.TagNo,
SellingRate = p.SellingRate
}))
.ToList();
If that's not supported by your LINQ provider (f.e. Linq-To-Entities
), the easiest is to use Linq-To-Objects
. To avoid that all is loaded into memory you should use AsEnumerable
after the Where
:
var result = inventoryDb.Pricing.AsNoTracking()
.Where(p => p.Quantity > 0m)
.AsEnumerable()
.SelectMany(p => Enumerable.Range(0, p.Quantity)
.Select(i => new
{
TagNo = p.TagNo,
SellingRate = p.SellingRate
}))
.ToList();
C# Determine Duplicate in List
Unless I'm missing something, then you should be able to get away with something simple using Distinct()
. Granted it won't be the most complex implementation you could come up with, but it will tell you if any duplicates get removed:
var list = new List<string>();
// Fill the list
if(list.Count != list.Distinct().Count())
{
// Duplicates exist
}
LINQ: Return non-duplicate Items in a List
So you want to remove all items which are duplicates?
You can use GroupBy
:
var distinctTest = test
.GroupBy(i => i)
.Where(g => g.Count() == 1)
.Select(g => g.Key);
C# Linq - Find Duplicate value in list and select it's id
You simply need to select the ORDERM_ID's from the group (and then flatten it with SelectMany)
var list = test
.GroupBy(x => new { x.BOOKING_DATE, x.PERIOD_NAME })
.Where(g => g.Count() > 1)
.SelectMany(g => g.Select(gg => gg.ORDERM_ID));
How to find custom duplicates entries within a list of objects using Linq?
You can use the linq query syntax which is very similar to sql that you wrote:
var duplicates =
from p1 in persons
from p2 in persons
where p1.Id != p2.Id && (
(p1.SSN == p2.SSN && p1.LastName == p2.LastName) ||
(p1.FirstName == p2.FirstName && p1.LastName == p2.LastName && p1.BirthDate == p2.BirthDate))
select new { Person1 = p1, Person2 = p2 };
c# - Using LinQ to look for duplicate items in a list and updating object properties if so
What you're looking for can be accomplished with GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>)
Using your object definiton you could do something like this
var consildatedFlights = allFlights.GroupBy(x => new {x.FlightNumber, x.Takeoff_time}, x => x,
(key, vals) => ConsolidateFlightInfo(vals));
foreach(var flight in consildatedFlights)
Console.WriteLine($"FlightNumber: {flight.FlightNumber}, Takeoff Time: {flight.Takeoff_time}, User Count: {flight.UserCount}");
public static FlightInfo ConsolidateFlightInfo(IEnumerable<FlightInfo> flights)
{
var list = flights.ToList();
var ret = list[0];
ret.UserCount = list.Sum(x => x.UserCount);
return ret;
}
The first argument to .GroupBy
specifies an anonymous type describing describing the properties you want to group by. The second item specifies what you want in your results lists (one per group). In this case we want the entire flight info object. The third parameter specifies how you want to transform each grouping. In this case we pass each set of grouped flights to a method that sums UserCount
and returns a single FlightInfo
with that summed value.
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.
Related Topics
Suppressing "Is Never Used" and "Is Never Assigned To" Warnings in C#
How to Get the File Size in C#
Make Listview.Scrollintoview Scroll the Item into the Center of the Listview (C#)
How to Get and Set the Window Position of Another Application in C#
Linq to SQL and a Running Total on Ordered Results
Retrieving the Calling Method Name from Within a Method
How to Convert Datatable to JSON String Using JSON.Net
Best Way to Display Decimal Without Trailing Zeroes
How to Post Data Using Httpclient
What Is the Real Overhead of Try/Catch in C#
Background Color of a Listbox Item (Windows Forms)
Why Does Enumerable.All Return True for an Empty Sequence
Automapper Convert from Multiple Sources