RavenDB - 索引和查询复杂的层次数据(嵌套属性)

时间:2017-02-03 17:59:28

标签: c# linq indexing ravendb querying

我有这样的博士:

public class Order
{
    public string ClientName { get; set; }
    public List<OrderItem> Items { get; set; }  
}

public class OrderItem
{
    public string ProductCode { get; set; }
    public int Quantity { get; set; }
}

我需要查询这些文件的集合:

var q = session.Query<Order_Index.Result, Order_Index>()
    .Where(o => o.ClientName.StartsWith("Jho") &&
                o.Items.Any(i => i.ProductCode == "Book" && i.Quantity >= 10))
    .OfType<Order>();

我发现的每个索引和查询层次数据的示例都只显示了在单独的查询表达式中只使用复杂嵌套对象的一个​​属性的情况。 e.g:

var q = session.Query<Order_Index.Result, Order_Index>()
    .Where(o => o.ClientName.StartsWith("Jho") &&
                o.ItemProductCodes.Any(c => c == "Book") &&
                o.ItemQuantities.Any(qty => qty >= 10))

但他们都没有考虑我的情况。

我尝试将ProductCode和Quantity编入索引作为单独的集合,然后在查询时通过集合索引将它们连接起来,但这会抛出某种Linq转换异常。我想知道,有没有机会在RavenDB中进行这种查询?

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Order> session = new List<Order>() {
                new Order() { ClientName = "Jho", Items = new List<OrderItem>() { 
                        new OrderItem() { ProductCode = "abc", Quantity = 1},
                        new OrderItem() { ProductCode = "def", Quantity = 4},
                        new OrderItem() { ProductCode = "Book", Quantity = 5},
                        new OrderItem() { ProductCode = "jkl", Quantity = 10}
                   }
                },
                new Order() { ClientName = "Mary", Items = new List<OrderItem>() { 
                        new OrderItem() { ProductCode = "mno", Quantity = 2},
                        new OrderItem() { ProductCode = "pqr", Quantity = 3},
                        new OrderItem() { ProductCode = "stu", Quantity = 4},
                        new OrderItem() { ProductCode = "vwx", Quantity = 5}
                    }
                },
                new Order() { ClientName = "Jho", Items = new List<OrderItem>() { 
                        new OrderItem() { ProductCode = "abc", Quantity = 28},
                        new OrderItem() { ProductCode = "cdf", Quantity = 7},
                        new OrderItem() { ProductCode = "Book", Quantity = 26},
                        new OrderItem() { ProductCode = "jkl", Quantity = 5}
                    }
                }
            };

            var q = session.Where(o => o.ClientName.StartsWith("Jho") && o.Items.Where(i => i.ProductCode == "Book" && i.Quantity >= 10).Any()).ToList();
        }
    }

    public class Order
    {
        public string ClientName { get; set; }
        public List<OrderItem> Items { get; set; }
    }

    public class OrderItem
    {
        public string ProductCode { get; set; }
        public int Quantity { get; set; }
    }

}