2011-11-18 55 views
1

嵌入文檔屬性的彙總資料,我有一個模型,它看起來像這樣如何獲得在RavenDB

public class User 
{ 
    public List<Action> Actions { get; set; } 
} 

public class Action 
{ 
    public DateTime CreatedOn { get; set; } 
} 

我試圖找到一種方式來獲得的CreatedOn所有行動最大值跨所有用戶。

我是RavenDB的新手,我不確定我是否應該使用Map/Reduce或什麼。

回答

4

下面是一個完整的示例:

public class User 
{ 
    public string Id { get; set; } 
    public List<Action> Actions { get; set; } 
} 

public class Action 
{ 
    public DateTime CreatedOn { get; set; } 
} 

public class ActionCreatedOnResult 
{ 
    public DateTime CreatedOn { get; set; } 
} 

public class Users_ActionCreatedOn : AbstractIndexCreationTask<User, ActionCreatedOnResult> 
{ 
    public Users_ActionCreatedOn() 
    { 
     Map = users => from user in users 
         from action in user.Actions 
         select new 
            { 
             action.CreatedOn 
            }; 
     Store(x => x.CreatedOn, FieldStorage.Yes); 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     using (var documentStore = new DocumentStore{ Url = "http://localhost:8080/" }) 
     { 
      documentStore.Initialize(); 

      IndexCreation.CreateIndexes(typeof(Users_ActionCreatedOn).Assembly, documentStore); 

      using (var documentSession = documentStore.OpenSession()) 
      { 
       var result = documentSession.Query<ActionCreatedOnResult, Users_ActionCreatedOn>() 
        .OrderByDescending(x => x.CreatedOn) 
        .AsProjection<ActionCreatedOnResult>() 
        .FirstOrDefault(); 
      } 
     } 
    } 
}