与RavenDB子文件查询列表 [英] Query list of sub documents with RavenDB

查看:137
本文介绍了与RavenDB子文件查询列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下对象结构:

public class Object
{
public string Id {get;set;}
public List<SubObject> SubObjects {get;set;}
}

public class SubObject {get;set;}
{
public string Id {get;set;}
public string Name {get;set;}
}

我将如何构建一个查询,以返回子对象的名单,其中 Name.Contains(A)

我觉得这应该是简单,但我真的与它挣扎着。

I feel like it should be straightforward but I'm really struggling with it.

推荐答案

在你的根对象,你需要存储的子对象的ID。然后,当你加载根对象,您需要使用乌鸦包括()功能,在您的查询。这将拉动所有的子对象文件到会话中。从那里,你就可以加载您的子对象。下面是如何我已经做了一个例子:

On your root object, you need to store the IDs of the subObjects. Then, when you load the root object, you need to use the Raven Include() functionality in your query. That will pull all of the subObject documents into the session. From there, you can then load your subObjects. Here is an example of how I've been doing it:

下面是一个根对象:

public class Application : EntityBase

应用程序有一个类型CustomVariableGroup的子对象。所以,我需要存储的ID(这是RavenDB将节省)的对象。

Application has subObjects of type CustomVariableGroup. So I need to store the IDs (that's what RavenDB will save) in the object.

public List<string> CustomVariableGroupIds { get; set; }  // For RavenDB

和实际的子对象仍然保存在根对象上,但不要保存在乌鸦这种方式:

And the actual subObjects are still stored on the root object, but don't get saved in Raven that way:

[JsonIgnore]  //  We do not want RavenDB to serialize this.
public ObservableCollection<CustomVariableGroup> CustomVariableGroups

这就是安装了,现在这里是它是如何处理的乌鸦。 (忘了的executeQuery()方法。这是我自己的那超出了这个问题的范围)

So that's the setup, now here's how it's dealt with in Raven. (Forget the ExecuteQuery() method; that's my own that's beyond the scope of this question.)

这是调用来获取根对象。请注意,包括(),我们拉的ID转换成会话:

This is the call to get the root object. Notice the Include() where we pull the IDs into the session:

public Application GetByName(string name)
{
    return ExecuteQuery<Application>(() =>
    {
        Application application = QuerySingleResultAndSetEtag(session =>
            session.Query<Application>()
            .Include(x => x.CustomVariableGroupIds)
            .Where(app => app.Name == name).FirstOrDefault())
            as Application;

        HydrateApplication(application);

        return application;
    });
}

这里是我们如何加载子对象:

And here is how we load the subObjects:

public static void HydrateApplication(Application app)
{
    if (app == null) { throw new ArgumentNullException("app"); }
    if (app.CustomVariableGroupIds == null) { return; }

    app.CustomVariableGroups = new ObservableCollection<CustomVariableGroup>();

    foreach (string groupId in app.CustomVariableGroupIds)
    {                
        app.CustomVariableGroups.Add(QuerySingleResultAndSetEtag(session => session.Load<CustomVariableGroup>(groupId)) as CustomVariableGroup);
    }
}

最后,保存根对象的时候,我一定要救子对象的ID:

Finally, when saving the root object, I make sure to save the subObject IDs:

private static void SetCustomVariableGroupIds(Application application)
{
    application.CustomVariableGroupIds = new List<string>();

    if (application.CustomVariableGroups == null || application.CustomVariableGroups.Count < 1) { return; }

    foreach (CustomVariableGroup group in application.CustomVariableGroups)
    {
        application.CustomVariableGroupIds.Add(group.Id);
    }
}

希望有所帮助。这使得在保持域模型纯,海事组织。实体可以持有引用其它实体,而不仅仅是非规范化其他实体的版本。

Hope that helps. This allows one to keep a domain model "pure," IMO. Entities can hold references to other entities, and not just denormalized versions of other entities.

好了,和真正回答你的问题。一旦你的文件被加载,你可以简单地使用LINQ查询的子对象,因为现在他们实际上是有在内存中。

Okay, and to actually answer your question... Once your document is loaded, you can simply use LINQ to query the subObjects, since now they will actually be there in memory.

这篇关于与RavenDB子文件查询列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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