2

I'm currently using a jagged array : private double[][] array; and I load the array like so.

Loading the jagged array

XDocument doc = XDocument.Load("file.Xml");
array = doc.Root.Elements("Month").Select(month => month.Elements().Select(x => (double)x).ToArray()).ToArray();

So now my problem is I need to get the lenght of the inner array and as I readed it's not possible, so I would need to load the xml in a rectangular array private double[,] array; but with linq I don't know how.

XML looks

<document>
  <Month>
    <Depth>-0.25</Depth>
    <October>0.95</October>
    <November>-0.90</November>
    ...
  </Month>
  <Month>
    <Depth>-0.5</Depth>
    <October>0.47</October>
    <November>-0.17</November>
    ...
  </Month>
  ...
</document>
8
  • Why it's not possible to get length of inner array? Commented Feb 4, 2014 at 13:52
  • Is Linq a requirement? Commented Feb 4, 2014 at 13:53
  • Well I get an exception when I try getLenght(1), and I,ve readed here stackoverflow.com/questions/2457507/… I think it's the last post Commented Feb 4, 2014 at 13:54
  • @C.Evenhuis well it need to be fast, since it's in an event, and I know linq do a good job, but no, it's not a requirement Commented Feb 4, 2014 at 13:55
  • there's no need to store it in an array Commented Feb 4, 2014 at 13:55

1 Answer 1

2
double[][] array = 
   doc.Root.Elements("Month")
      .Select(month => month.Elements().Select(x => (double)x).ToArray())
      .ToArray();

foreach(var innerArray in array)
    Console.WriteLine(innerArray.Length); // you can get length of inner array
Sign up to request clarification or add additional context in comments.

5 Comments

I can't permit to do a foreach since it would be called on mousemove and it would constantly get called if the suer move in the canva
@Mokmeuh sorry didn't get you. Can you explain what's wrong with foreach? And what exactly you want to get from array?
Sorry if I was not clear, let me try again, what I meant is, the foreach would be called every time the mouse mouve so it would take alot of ressources, and the lenght is needed in a ternary conditional like so, array[yCurrent >= array.GetLength(1) ? 11 : yCurrent][xCurrent >= array.GetLength(0) ? 12 : xCurrent] but I can't get the lenght of the dimmension 1 since it doesn't exist because it's not a rectangular array
@Mokmeuh you are not forced to enumerate array. That was just an example of accessing inner array length. You can do same via accessing by index array[0].Length - that will give you length of first inner array
.... I'm dumb xD, let me try this, and it's working, alright, thank you, I guess I was asking the wrong question

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.