0

要么是我有心理障碍,要么不是那么简单。

我有 2 节课,类似这样:

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 类中有一个 City 会很容易,但我没有,而且它在那里没有意义,imo。

帮助 ?

编辑:

我想出了这个索引,它实际上适用于内存列表,但 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 });
        }
    }
4

1 回答 1

1

您可以使用MultiMap 索引来做到这一点- 但 RavenDB 2.0 中有一个很棒的新功能,称为索引相关文档,它更容易。

Map = people => from person in people
                let house = LoadDocument<House>(person.HouseId)
                select new
                {
                    house.City,
                    house.HouseNumber,
                    PersonName = person.Name,
                }
于 2013-01-11T00:16:46.593 回答