2017-03-28 25 views

回答

1

我想创建DTO类您的收藏:

public class Restaurant 
{ 
    public ObjectId _id { get; set; } 
    public address address { get; set; } 
    public string borough { get; set; } 
    public string cuisine { get; set; } 
    public grades[] grades {get;set;} 
    public string name { get; set; } 
    public string restaurant_id {get;set;} 
} 

public class grades 
{ 
    public DateTime date {get;set;} 
    public string grade {get;set;} 
    public int? score {get;set;} 
} 

public class address 
{ 
    public string building { get; set; } 
    public double[] coord { get; set; } 
    public string street { get; set; } 
    public string zipcode { get; set;} 
} 

如果你创建你的收藏如:

var collection = db.GetCollection<Restaurant>("restaurants"); 

,你可以只是为了你的结果是这样的:

collection 
    .Aggregate() 
    .Project(r => new {Restarant = r.name, TotalScore= r.grades.Sum(g=>g.score)}) 
    .SortByDescending(x=>x.TotalScore) 
    .ToList()