How to do a count on an IEnumerable? [duplicate] - c#

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

Related

Searching for two same values in one array [duplicate]

This question already has answers here:
C# LINQ find duplicates in List
(13 answers)
Closed 5 years ago.
I was hoping to do something like:
int[] locations;
do
{/*edit locations to only contain one int in array of same value*/}
while (locations.Where(val, secondVal => val == secondVal).count > 1)
And I highly feel like I've done something of the sort in the past, perhaps it was with a list and it doesn't function the same way with an array but I can't seem to find my example anywhere.
I feel that the code snip explains well enough my intentions, but it doesn't work, would someone be able to help? Thanks.
I'll keep searching online but if someone could help it would obviously speed up my search.
You should look to this answer:
C# LINQ find duplicates in List
Just group elements by its value and then get the list
GroupBy(x=>x).Where(g=>g.Count()>1)

Recursion, and the difference between -- and i-1 [duplicate]

This question already has answers here:
pre Decrement vs. post Decrement
(2 answers)
Closed 6 years ago.
I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--. When I changed it to index - 1 it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1. I was wondering if anyone could explain the reason behind this behavior.
Post-decrement operator returns the value before decrementing, so in your case the index will never be 0 and the function won't stop calling itself and you'll get a stack overflow. You want to write --index instead. It will return the value after decrementing then.

Check two IEnumerable if their Count is equal [duplicate]

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.

C# Check if any int in list matches any int in another list [duplicate]

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.

how to select elements that not intersec? [duplicate]

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

Categories

Resources