2012-05-03 46 views
0

我想要获得一个使用RavenDB的MVC应用程序的所有剧院名称的列表。所以我期待着从嵌套对象一个ravendb文件...在列表中获取嵌套的属性 - 使用索引

样品RavenDB文件:

{ 
    "City": "NY", 

    "Theaters": [ 
     { 
      "TheaterName": "TheaterA", 
      "TheaterId": "Theater/1693", 
      "Description": ""    
     }, 
     { 
      "TheaterName": "TheaterB", 
      "TheaterId": "Theater/16393", 
      "Description": ""    
     } 
    ] 
} 

指数(GetCityTheatersIndex):

docs.CityTheaters 
    .Select(doc => new {Theaters = doc.Theaters, City= doc.City}) 

代码:

var query = 

session.Advanced.LuceneQuery<CityTheaters>("CityTheaters/GetCityTheatersIndex"); 

如何操纵上面的变量“查询”以获得一个只是TheaterNames的列表..现在上面的代码让我在变量“查询”中的整个文档..我需要能够解析出嵌套对象中的“影院名称”并填充到列表中..感谢您的任何帮助。

我想是这样..

query.ElementAt(1)..但没得到我的任何地方。

回答

3

您可以使用地图索引与字段存储要做到这一点:

public class CityTheaters : AbstractIndexCreationTask<City> 
{ 
    public class Result 
    { 
     public string Name { get; set; } 
    } 

    public CityTheaters() 
    { 
     Map = cities => from city in cities 
         from theater in city.Theaters 
         select new 
         { 
          theater.Name 
         }; 

     Store(x => x.Name, FieldStorage.Yes); 
    } 
} 

后来,你可以查询这样的:

var results = session.Query<City, CityTheaters>() 
    .AsProjection<CityTheaters.Result>() 
    .ToList(); 
+0

我不能得到(x.Name在商店X = > x.Name ...)最后一行代码... intellisense只给我选项..编号,城市和剧院.. – ZVenue

+0

如果您使用AbstractIndexCreationTask 定义您的索引,那很正常。不要在AbstractIndexCreationTask泛型定义中指定Reduced Result。如Daniel所说,只使用AbstractIndexCreationTask ,您将在Store()上获得x.Name。 –

+0

是的,我只做AbstractIndexCreationTask ..它不给我的x.name – ZVenue