Arrays of Arrays

Feb 28, 2012 at 3:13 PM

(Taken from my StackOverflow question)

Consider the difference between these two lines:

$('tr'); // returns array of tr
('tr').toEnumerable().TojQuery(); // returns array of tr[1]

Converting from jQuery to Enumerable and back to jQuery does not give you what you started with. The end result is an array of arrays of elements, with each sub-array having a length of 1. I do need to make use of Enumerable, so this is just a convenient example of my problem.

This means that to get the id of an element, you'd need to do the following:

$('tr')[0].id; // returns "myID"
('tr').toEnumerable().TojQuery()[0][0].id; // returns "myID"

I'm surprised of this, because even though I've allegedly gone back TojQuery(), the object returned by TojQuery() does not work with typical jQuery calls:

$('tr').find('td').length; // returns 170 (in my case)
('tr').toEnumerable().TojQuery().find('td').length; // returns 0 (BAD)

I would like it if both lines returned 170, but apparently Linq-For-Javascript doesn't work that way.

So, my questions:

  1. Why is this?
  2. Am I doing it wrong?
  3. If not, any good workarounds? (convert array of 1-element arrays to array of elements?)