Overload of "Contains" in C# [duplicate] - c#

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

Related

How to look through arrays? [duplicate]

This question already has answers here:
filter an array in C#
(6 answers)
Closed 5 years ago.
I want a way to look through an array with a certain criteria.
For example,
str[] balance = { hi.Text, bye.Text, hello.Text, what.Text };
How do I look through it with the criteria of "more than 4 characters" so it would look at each one, and skip it if it doesn't meet the criteria.
So like in this example, it would skip hi and bye, but not hello.
var fourOrMore = balance.Where(x => x.Length > 4).ToList();
Would do exactly what you need, you would need to add using System.Linq; to you class file

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.

C# Linq Query to get between items [duplicate]

This question already has answers here:
Get previous and next item in a IEnumerable using LINQ
(11 answers)
Closed 9 years ago.
Hi I have a dictionary like this
("ABC","X")
("CDE","C")
("EFG","X")
I need to retrieve the items which are in two sides of "C" , How to achieve this with linq ?
Output Expected :
Item = n / output = (n-1)(n+1) items
("ABC","X") and ("EFG","X")
Something like this?
Dict.TakeWhile(e => e.Value = "C").Take(1).Reverse().Take(3).
Then remove the middle one.

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