RavenDb:查询相关实体中的财产

时间:2013-01-10 23:53:52

标签: ravendb

要么我有心理障碍,要么不是那么直截了当。

我有两个类,类似的东西:

public class House
{
    public string Id { get; set; }
    public string City { get; set; }
    public string HouseNumber { get; set; }
}

public class Person
{
    public string Id { get; set; }
    public string HouseId { get; set; }
    public string Name { get; set; }
}

现在,我想要一个生活在特定城市的所有人的列表,在平面模型中({City,HouseNumber,PersonName})。

我无法找到一种如何映射的方法..如果我在Person类中有一个容易的城市,但我没有,并且那里没有意义,imo。< / p>

帮助?

编辑:

我想出了这个索引,它实际上与内存列表一起使用,但是Raven什么都不返回:(

 public class PeopleLocations : AbstractMultiMapIndexCreationTask<PeopleLocations.EntryLocation>
    {
        public class PeopleLocation
        {
            public string PersonId { get; set; }
            public string HouseId { get; set; }
            public string City { get; set; }
        }


        public PeopleLocations()
        {
            this.AddMap<House>(venues => venues.Select(x => new
            {
                x.City,
                HouseId = x.Id,
                PersonId = (string)null
            }));

            this.AddMap<Person>(people => people.Select(x => new
            {
                City = (string)null,
                HouseId = x.HouseId,
                PersonId = x.Id
            }));

            this.Reduce = results => results.GroupBy(x => x.HouseId)
                .Select(x => new
                {
                    HouseId = x.Key,
                    People = x.Select(e => e.PersonId),
                    City = x.FirstOrDefault(y => y.City != null).City,
                })
            .SelectMany(x =>
                x.People.Select(person => new PeopleLocation
                {
                    PersonId = person,
                    HouseId = x.HouseId,
                    City = x.City,
                })
            )
            .Select(x => new { PersonId = x.PersonId, x.City, x.HouseId });
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以使用MultiMap Index执行此操作 - 但RavenDB 2.0中有一个名为Indexing Related Documents的新功能,更加容易。

Map = people => from person in people
                let house = LoadDocument<House>(person.HouseId)
                select new
                {
                    house.City,
                    house.HouseNumber,
                    PersonName = person.Name,
                }