2013-01-10 24 views
0

要麼我有心理障礙,要麼不那麼直截了當。RavenDb:通過相關實體的房產查詢

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

現在我想生活在某個城市所有的人的名單,在扁平模型({市HouseNumber,PERSONNAME})。

我無法弄清楚如何映射該方法。如果我在Person類中有一個城市,這很容易,但是我沒有,並且在那裏沒有意義,imo。

幫助?

編輯:

我想出了這個指標,這實際上與在內存中的列表的工作原理,但烏鴉沒有返回:(

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

您可以用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, 
       } 
+0

我會嘗試升級,Raven似乎不喜歡我的multimap .. – Evgeni