RavenDB - 索引和查询复杂的层次数据(嵌套属性) [英] RavenDB - Indexing and querying complex hierarhical data (nested properties)

查看:175
本文介绍了RavenDB - 索引和查询复杂的层次数据(嵌套属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文档:

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; }
}

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

And I need to query collection of theese documents like this:

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>();

我发现的每个索引和查询层次数据的例子只显示了一个案例,只有一个属性复杂嵌套对象的使用在单独的查询表达式中。例如:

Every example of indexing and querying hierarhical data, that I found, shows only case, when only one property of complex nested object is used in separate query expression. 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))

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

but none of them consider my situation.

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

I tried to index ProductCode and Quantity as separate collections and then join them by collection index while querying, but this throws some kind of Linq translation exception. I wonder, is there any opportunity to do this kind of querying in RavenDB?

推荐答案

试试以下内容:

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; }
    }

}

这篇关于RavenDB - 索引和查询复杂的层次数据(嵌套属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆