Example #1
0
		public void QueryDescendants_MatchingPropertyToArray_ReturnsArray()
		{
			var input = @"
{
	""name"" : ""Root Object"",
	""matchThis"" : ""find me."",
	""children"" : [
		{
			""name"" : ""Skipped Child"",
			""matchThis"": ""skip me."",
			""children"" : [
				{
					""name"" : ""Skipped Grandchild 1"",
					""matchThis"": ""skip me.""
				},
				{
					""name"" : ""Matching Grandchild 1"",
					""matchThis"": ""find me.""
				}
			]
		},
		{
			""name"" : ""Matching Child"",
			""matchThis"": ""find me."",
			""children"" : [
				{
					""name"" : ""Matching Grandchild 2"",
					""matchThis"": ""find me.""
				},
				{
					""name"" : ""Skipped Grandchild 2"",
					""matchThis"": ""skip me.""
				}
			]
		}
	]
}";

			// returns in document order
			var expected = new[]
				{
					new
					{
						Name = "Matching Grandchild 1"
					},
					new
					{
						Name = "Matching Child"
					},
					new
					{
						Name = "Matching Grandchild 2"
					}
				};

			// define an anonymous object which contains only the fields we want to retrieve
			var template = new { name=String.Empty, matchThis=String.Empty };

			var source = new JsonReader().Query(input, template);

			var query =
				from descendant in source.Descendants()
				where (descendant.matchThis == "find me.")
				select new
				{
					Name = descendant.name
				};

			var actual = query.ToArray();

			Assert.Equal(expected, actual, true);
		}