2014-10-09 81 views
0

我與實體框架6工作(EF 6),並具有以下型號:實體框架6 - 的GroupBy對導航性能

public class Engine { 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Car> Cars { get; set; } 
} 

public class Car { 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public virtual Engine Engine { get; set; } 
    public virtual Purchase Purchase { get; set; } 
} 

public class Purchase { 
    public int Id { get; set; } 
    public DateTime Created { get; set; } 

    public virtual ICollection<Car> Cars { get; set; } 
} 
  • Engine <-> Car(1對多的關係)
  • Purchase <-> Car(1對多的關係)

怎麼可能確定哪些發動機購買的是什麼?我需要一個有序的列表(降序)引擎以及它們被購買的頻率。

我在導航屬性上存在一些問題,以及如何對它們進行適當的分組/聚合。

感謝

回答

0

試試這個:

 return 
      (from car 
       in this.Context.Cars 
        where car.Purchase != null 
        group car by car.Engine into g 
       select new { EngineName = g.Key.Name, CountSold = g.Count()}) 
      .OrderByDescending(x => x.CountSold) 
      .ToList(); 

另外,請看看here