public void Convert_select_many_will_keep_doc_id()
        {
            IndexDefinition indexDefinition = new IndexDefinition<Order>
            {
                Map = orders => from order in orders
                                from line in order.OrderLines
                                select new { line.ProductId }
            }.ToIndexDefinition(new DocumentConvention());
			var generator = new DynamicViewCompiler("test", indexDefinition, new AbstractDynamicCompilationExtension[0])
                .GenerateInstance();


            var results = generator.MapDefinition(new[]
			{
				GetDocumentFromString(
				@"
                {
                    '@metadata': {'Raven-Entity-Name': 'Orders', '@id': 1},
                    'OrderLines': [{'ProductId': 2}, {'ProductId': 3}]
                }"),
				  GetDocumentFromString(
				@"
                {
                    '@metadata': {'Raven-Entity-Name': 'Orders', '@id': 2},
                    'OrderLines': [{'ProductId': 5}, {'ProductId': 4}]
                }")
			}).Cast<object>().ToArray();

            foreach (var result in results)
            {
                Assert.NotNull(TypeDescriptor.GetProperties(result).Find("__document_id", true));
            }
        }
        public void Can_compile_map_reudce_using_linq_methods()
        {
            var viewGenerator = new DynamicViewCompiler("test", new IndexDefinition
            {
                Map = @"docs.Users
            .Select(user => new {Location = user.Location, Count = 1})",
                Reduce =
                    @"results
            .GroupBy(agg => agg.Location)
            .Select(g => new {Loction = g.Key, Count = g.Sum(x => x.Count}))"
            }).GenerateInstance();

            var results = viewGenerator.ReduceDefinition(viewGenerator.MapDefinition(new[]
            {
                GetDocumentFromString(
                @"
                {
                    '@metadata': {'Raven-Entity-Name': 'Users', '@id': 1},
                    'Location': 'Tel Aviv'
                }"),
                  GetDocumentFromString(
                @"
                {
                    '@metadata': {'Raven-Entity-Name': 'Users', '@id': 1},
                    'Location': 'Tel Aviv'
                }")
            })).Cast<object>().ToArray();

            var expected = new[]
            {
                "{ Loction = Tel Aviv, Count = 2 }",
            };

            for (var i = 0; i < results.Length; i++)
            {
                Assert.Equal(expected[i], results[i].ToString());
            }
        }
		public void Can_compile_map_using_linq_methods()
		{
			var viewGenerator = new DynamicViewCompiler("test", new IndexDefinition
			{
				Map = @"docs.Users
	.Select(user => new {Location = user.Location, Count = 1})
	.Select(user => new {Location = user.Location})"
			}, new AbstractDynamicCompilationExtension[0]).GenerateInstance();


			var results = viewGenerator.MapDefinition(new[]
			{
				GetDocumentFromString(
				@"
                {
                    '@metadata': {'Raven-Entity-Name': 'Users', '@id': 1},
                    'Location': 'Tel Aviv'
                }")
			}).Cast<object>().ToArray();

			var expected = new[]
			{
				"{ Location = Tel Aviv, __document_id = 1 }",
			};

			for (var i = 0; i < results.Length; i++)
			{
				Assert.Equal(expected[i], results[i].ToString());
			}
		}
		public void LinqQueryWithStaticCallOnEnumerableIsCanBeCompiledAndRun()
		{
			var indexDefinition = new IndexDefinition<Page>
			{
				Map = pages => from p in pages
							   from coAuthor in p.CoAuthors.DefaultIfEmpty()
							   select new
							   {
								   p.Id,
								   CoAuthorUserID = coAuthor != null ? coAuthor.UserId : -1
							   }
			}.ToIndexDefinition(new DocumentConvention());

			var mapInstance = new DynamicViewCompiler("testView",
													  indexDefinition, new AbstractDynamicCompilationExtension[] { }).
				GenerateInstance();

			var conventions = new DocumentConvention();
			var o = JObject.FromObject(page,conventions.CreateSerializer());
			o["@metadata"] = new JObject(
				new JProperty("Raven-Entity-Name", "Pages")
				);
			dynamic dynamicObject = new DynamicJsonObject(o);

			var result = mapInstance.MapDefinition(new[] { dynamicObject }).ToList();
			Assert.Equal("{ Id = 0, CoAuthorUserID = 1, __document_id =  }", result[0].ToString());
			Assert.Equal("{ Id = 0, CoAuthorUserID = 2, __document_id =  }", result[1].ToString());
		}