This question already has answers here:
Using Linq with 2D array, Select not found
(2 answers)
Closed 7 years ago.
What is the proper way to handle a 2D array with LINQ?
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
bool anyZeroes = array.Any(value => value == 0) // example
I want to check if any variable in the array matches a Func, == 0 in this case. How can I use Any for this and what is the best practice here?
Here's a way you can flatten the list to check
bool anyZeroes = array.Cast<int>().Any(value => value == 0);// false
bool anyNines = array.Cast<int>().Any(value => value == 9);// true
Though, if you are making multiple calls you should store it:
bool casted = array.Cast<int>();
bool anyZeroes = casted.Any(value => value == 0);// false
bool anyNines = casted.Any(value => value == 9);// true
Reference: https://stackoverflow.com/a/13822900/526704
Related
This question already has answers here:
Easiest way to compare arrays in C#
(19 answers)
Closed 3 years ago.
Is there a way to use an if statement to test an entire array at once by doing something like this:
if(myArray == {1,2,3})
{Debug.Log("This is quick")}
or do I need to iterate through each value in the array like this:
if(myArray[0] == 1 && myArray[1] == 2 && myArray[2] == 3)
{Debug.Log("This is not as quick")}
You should use SequenceEqual.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int [] myArray = {1,2,3};
int [] myArray2 = {1,2,4};
bool result = myArray.SequenceEqual(myArray2);
}
}
This question already has answers here:
Why does Enumerable.All return true for an empty sequence? [duplicate]
(7 answers)
Closed 7 years ago.
Consider the following code:
static void Main(string[] args)
{
List<string> items = new List<string>();
string result = null;
if(items.All(o => o == "ABC"))
{
result = "All";
}
else if(items.Any(o => o == "XYZ"))
{
result = "Any";
}
Console.WriteLine(result);
Console.Read();
}
This prints "All".
Why does an empty list satisfy an "All" condition where o == "ABC"
According to MSDN:-
Enumerable.All
Return true if every element of the source sequence passes the
test in the specified predicate, or if the sequence is empty;
otherwise, false.
So in your case since items is an empty collection it is returning true.
This is by design and also consistent with how the universal quantifier ∀ works in mathematics on sets.
I know I can do it like this:
if(myint == 1 || myint == 2 || myint ==3) //etc...
But I feel like there must be a more efficient way to code this. Is there a way to possibly make a statement like this work?
if(myint.Contains(1 || 2 || 3 || 4))
you can do inverse
new List<int>{1,2,3,4}.Contains(myInt)
Note that there is also Enumerable.Any, but Contains would work for .net 2.0 too.
Close, try the following.
It will take a collection and return true if your int is in the collection:
if (new[] { 1, 2, 3, 4 }.Contains(myint))
//Do something
new[] { 1, 2, 3, 4 } represents an array of integers.
The Contains method is an extension of IEnumerable<T> and will be available to anything that implements it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Comparing two List<string> for equality
How can I find out whether two arrays of string are equal to each other?
I used this, but it does not work, even though the contents of both are the same.
string[] array1 = new string[]{"A" , "B"}
string[] array2 = new string[]{"A" , "B"}
if(array1 == array2) // it return false !!!!
{
//
}
If you have access to Linq, use SequenceEqual. Otherwise, simply provide the code to first check if the arrays are equal length, and then if items are equal at each index.
Have a look at the following on StackOverflow. I believe what you are looking for is the following. Comparing arrays in C#
var arraysAreEqual = Enumerable.SequenceEqual(array1, array2);
static bool ArraysEqual<T>(T[] a1, T[] a2)
{
if (ReferenceEquals(a1,a2))
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
for (int i = 0; i < a1.Length; i++)
{
if (!comparer.Equals(a1[i], a2[i])) return false;
}
return true;
}
You can use the .NET4 feature Array.IStructuralEquatable.Equals like this:
IStructuralEquatable equ = array1;
bool areEqual = equ.Equals(array2, EqualityComparer<string>.Default);
This can also be written on one line:
bool areEqual = (array1 as IStructuralEquatable).Equals(array2, EqualityComparer<string>.Default);
Using IStructuralEquatable has the advantage of allowing a custom comparer to be used.
This question already has answers here:
Check whether an array is a subset of another
(10 answers)
Closed 8 years ago.
Given two sets of values:
var subset = new[] { 2, 4, 6, 8 };
var superset = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
how do I determine if superset contains all elements of subset?
I have come up with this:
superset.Intersect(subset).Count() == subset.Count()
Is this the most logical and efficient method?
Count? How about Not Any?
bool contained = !subset.Except(superset).Any();
So, my other answer was pretty easy to use. But it's an O(n*m) solution.
Here's a slightly less friendly O(n+m) solution. This should be used if superset is HUGE. It avoids repeatedly enumerating superset.
HashSet<int> hashSet = new HashSet<int>(superset);
bool contained = subset.All(i => hashSet.Contains(i));
I have an extension method that uses the existing Contains()-method. I find it more intuitive than using Instersect() or Except().
public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
{
return values.All(value => source.Contains(value));
}
You could use Except and the resulting count should be 0.
Read up on MSDN for details of the parameters.
Example:
subset.Except(superset).Count() == 0