2
[0,0,0]
[1,1,1]
[2,2,2]

I have the above 2 dimensional array.

I need to check for 3 things, first is to check if all the cells are filled just like above.

Second:

[0,0,0]
[1]
[]

For the above array, I need to check if all the cells are populated per row.

Third:

[0,]
[1,1]
[2,2,2]

I want to find if the first elements of first column are populated.

I can do it with foreach loops, or for loop. but i want to use All(predicate) with linq.

            foreach (var ticketValue in ticketValues)
            {
                firstRow = ticketValue.All(x => x == i);

                foreach (var value in ticketValue)
                {

                }
            } 

What would be the best way to do this?

10
  • 6
    I'm not sure what you exactly want to do. Could you either list the expected outputs or write the foreach version of what you want to do? Commented Jul 16, 2012 at 4:43
  • 1
    Do you mean to say 3 dimensional array? Commented Jul 16, 2012 at 13:04
  • Might be good to give some code examples, because your description is very vague. I gather that the first array is some sample data [not array declaration] but the second example is FUBAR? How can a row element NOT be populated from the description given [int, 2d array] it MUST be populated. Commented Jul 16, 2012 at 13:18
  • 1
    @chridam It's 2-dimensional. It only has 2 axes. Commented Jul 16, 2012 at 13:27
  • 1
    @NPSF3000 It looks like a jagged array, not a rectangular one. Commented Jul 16, 2012 at 13:45

1 Answer 1

4

I'm assuming you're setting up that 2D like this:

int?[][] myArrayA = new int?[][] { new int?[] {0,0,0}, new int?[] {1,1,1}, new int?[] {2,2,2} };
int?[][] myArrayB = new int?[][] { new int?[] {0,0,0}, new int?[] {1}, new int?[] {null} };
int?[][] myArrayC = new int?[][] { new int?[] {0,null}, new int?[] {1,1,1}, new int?[] {2,2,2} };

So, using Linq we do this:

bool FirstCheck(int?[][] theArray)
{
    int size = (from arrays in theArray select arrays.GetUpperBound(0)).Max();

    var check = from arrays in theArray
                where theArray.All(sub => sub.GetUpperBound(0) == size)
                select arrays;

    return size + 1 == check.Count<int?[]>();
}

bool SecondCheck(int?[][] theArray)
{
    int size = (from arrays in theArray select arrays.GetUpperBound(0)).Max();

    var check = from arrays in
                    (from subs in theArray
                     where theArray.All(sub => sub.All(value => value != null))
                     select subs)
                where arrays.GetUpperBound(0) == size
                select arrays;

    return size + 1 == check.Count<int?[]>();
}

bool ThirdCheck(int?[][] theArray)
{
   int size = (from arrays in theArray select arrays.GetUpperBound(0)).Max();

   var check = from arrays in theArray
               where theArray.All(array => array[0].HasValue)
               select arrays;

   return size + 1 == check.Count<int?[]>();
}

Hope that's what you were wanting...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.