Hashem spoke to Moshe in Midbar Sinai... Take a census of the entire assembly of the Children of Israel according to their families, according to their fathers’ household...
The Yalkut Shimoni (רמז תרפ"ד) comes to explain why the Torah discusses this counting of the Jewish Nation right here in the beginning of Parshas Bamidbar. At the time that Klal Yisroel accepted the Torah, the nations of the world grew envious, and complained, “Why did Hashem see it fit to draw the Jewish Nation close to him more than all other nations of the world?” Hashem silenced them by responding, “Bring me your books of lineage, just as the Jewish people brought me their book of lineage”. This is why Hashem counted the Jewish Nation right at the beginning of Parshas Bamidbar - immediately following the last Passuk of Parshas Bechukosai where the Torah recounts their receiving the Torah at Har Sinai as it says; ‘These are the Mitzvos that Hashem commanded Moshe to the Children of Israel on Har Sinai’ - because the Jews only merited to receive the Torah due to their lineage.
We need to understand, firstly, why Hashem needed to subdue them when there was absolutely no merit to their complaint, for they themselves were also given the choice to accept the Torah but decided to refuse the offer, so how can they come now with complaints of envy? Secondly, how did Hashem silence them by telling them to bring their books of genealogy; what does that have to do with the discussion at hand?
We can explain the Nations’ complaint as follows. The nations just couldn’t comprehend how the Jews were able to find within themselves the resolve to accept the Torah, after hearing every other nation reject it due to the adversity which they all, mistakenly, claimed it to entail. Hence, they felt that it ought to be that the Jews must have some personal benefit to gain by accepting the Torah, which is what caused them to ignore the “fact” that by accepting the Torah they would be putting themselves into a very difficult and challenging lifestyle. The advantage that the worldly nations felt the Jewish Nation stood to gain by accepting the Torah, was of proving their worth and purpose to the other nations. For the nations felt, that because the Jews were descendants of Yaakov, who was the younger of the two brothers, they therefore must feel themselves to be of lesser value and esteem than the other nations, who were descendants of Eisav, the older of the two. The nations assumed that as a result of this feeling of inferiority, the Jews felt the need to strengthen their self-image and to prove themselves and their worth to all the other nations. Thus, when the offer to accept the Torah was laid before them, they felt this to be the perfect opportunity to do just that, by showing the other nations that they’re better and more resolved than them. Accordingly, the nations came to Hashem to complain that because the Jews only accepted the Torah for personal gain, and it had nothing to do with their love for Hashem, therefore it wasn’t right that their acceptance of the Torah should bring about such a strong love and bond between Hashem and the Jewish Nation.
When Hashem heard their complaint, in which they claimed that the Jews accepted the Torah only because of their desperate feelings of inferiority due to them originating from the younger of the brothers, Hashem silenced them, for he told them that they are just plainly mistaken. Hashem told them, “Bring me your books of lineage, just as the Jewish people brought me their book of lineage and let us see who among the nations stem from the older and more prominent of the two brothers”. When checking out the lineage book, one would find an entirely different setting, for although indeed Eisav was born first and thus was the older of the two, nevertheless there came a time when Eisav sold his seniority to his younger brother Yaakov, and thereafter Yaakov was considered to be older and thus superior of the two. Consequently, the nations were obviously gravely mistaken, as the Jewish people had no need to prove themselves. Thus, we must say that they accepted the Torah out of their love to Hashem, and because of their trust in Him that He would never offer them anything which wasn’t in their best interest. Therefore, the Jewish Nation was definitely worthy of Hashem’s love, and of having that special bond that only the Jews share with Hashem.