This question already has answers here:
removing duplicates in a list with linq
(3 answers)
Closed 8 years ago.
How do I remove dupliactes in a collection? For example:
List<double> somelist=new List<double>{2, 2, 3};
What LINQ method should I use?
And try to make it simple please.
Enumerable.Distinct is probably what you're looking for.
var somelist = new List<double>{2, 2, 3}.Distinct();
// somelist now contains 2, 3
I would suggest using a HashSet<double> instead of List<double>.
Related
This question already has answers here:
LINQ compare two lists and remove
(4 answers)
Closed 8 months ago.
I am new to c# development. I want to compare and remove list items that already exist in another list. Is there any built in method to do that instead of doing it using loop?
The List object has a RemoveAll method which can remove all elements of the list which match the predicate.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeall?view=net-6.0
You can also use the except method, answered here: Remove items from one list in another
var list1 = { /* first list */ };
var list2 = { /* second list to remove from first */ };
list1 = list1.Except(list2).ToList();
This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);
This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 8 years ago.
Distinct() is not working. It displays all the repeating values.
I searched for a solution but just got more confused. I tried this :
var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();
Is there a quick uncomplicated way to make this work?
use GroupBy instead of Distinct
This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
I have two sting lists
List<string> list1=new List(){"1","2","3"};
List<string> list2=new List(){"1","2"};
What will be the easiest way to check if list1 contains the values in list2.
How about
list1.Except(list2).Any();
Try using
[listName].Except(SecondListName).Any();
This should work.
This question already has answers here:
IEnumerable vs List - What to Use? How do they work?
(11 answers)
IList vs IEnumerable for Collections on Entities
(2 answers)
Closed 9 years ago.
I am confused. Can anybody help me to understand Difference between IEnumeration<T> instead and List<T>?
You mean IEnumerable<T>. It's the base interface of all collection types like arrays or generic List<T>.
You can for example create a List<int>:
List<int> ints = new List<int>(){ 1,2,3 };
But since it implements IEnumerable<T> you could also declare it in this way:
IEnumerable<int> ints = new List<int>(){ 1,2,3 };
This has the advantage that you cannot modify ints since Remove comes from ICollection<T>.