This question already has answers here:
Count the items from a IEnumerable<T> without iterating?
(21 answers)
Closed 8 years ago.
How to check two IEnumerables whether they have same count without running through them individually. Means I do not want to do this Count() == Count().
I would like to find a way to do that in one pass. Any ideas?
This is not possible. Whatever approach you choose, you will have to run through both sequences.
The most straightforward way is to use the Count() method, where would be an O(1) if both sequences are List. In this case, the Count() fails to get the value of list's property called Count.
Related
This question already has answers here:
The best way to get a count of IEnumerable<T>
(11 answers)
Closed 2 years ago.
I want to be able to test a count of obj below but not able to do so.
when I mouse over obj it shows a count of 3
<IEnumerable<File> obj = await _mytester.cleanup(myitems)
why cant i do this?
if(obj.count > 0)
The liine above is giving me an error
You can't do that because count doesn't exist on IEnumerable and that's by design.
You can use IEnumerable.Count() like this:
if (obj.Count() > 0)
But that's horribly inefficient since you're really just checking if there's anything there.
You're better off doing this:
if (obj.Any())
Both of these are provided by LINQ extension methods.
It's very important to note that Count() is iterating through all elements and is nowhere near as efficient as something like ICollection.Count
This question already has answers here:
What is a method group in C#?
(5 answers)
Closed 4 years ago.
I am using the below code to check if two lists are equal.
var a = ints1.All(ints2.Contains) && ints1.Count == ints2.Count;
The only thing I do not understand is how does ints2.Contains work. As far as I know, Contains() is a method and takes a parameter. As we can see here, Contains is not taking any parameter.
Note - ints1 and ints2 are two different lists.
That's called a method group. It's basically a shorcut for this:
ints1.All(x => ints2.Contains(x))
This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1
This question already has answers here:
How to find if an element of a list is in another list?
(5 answers)
Closed 9 years ago.
I apologize if this is an obvious question, but I cannot find the answer.
Say I have the following:
var list1 = new List<int>{1,2,3};
var list2 = new List<int>{3,5,6};
How can I see if ANY element of list1 is contained in list2? So in this case I want to return true because 3 is in both.
Performing nested loops will not work for me, so it would be ideal if there was a:
list1.HasElementIn(list2);
Use Enumerable.Intersect - it produces intersection of both sequences. If intersection is not empty, then there some item which exists in both sequences:
bool isAnyItemInBothLists = list1.Intersect(list2).Any();
One thing to note - thus Intersect is a deferred streaming operator, then you will get result as soon as any common item will be found. So, you don't need to wait until complete intersection will be computed.
This question already has answers here:
Get the symmetric difference from generic lists
(7 answers)
The opposite of Intersect()
(8 answers)
Closed 9 years ago.
If I have two list and I want the elements that are common in both lists, I can use this code:
var listC = listA.Intersect(listB);
However, If I want the elements that are not common? And without duplicates? is possible with intersect?
Thanks.
Neither answer so far will include items from listB that aren't in listA. To get any item that is in either list, but is not in both lists:
listA.Union(listB).Except(listA.Intersect(listB));
Yep, that's possible. It's called Enumerable.Except.
Use this:
var result = listA.Except(listB); //maybe a .ToList() at the end,
//or passing an IEqualityComparer<T> if you want a different equality comparison.
Most efficient:
var set = new HashSet<T>(listA);
set.SymmetricExceptWith(listB);