3

從模型計算,我有以下型號:獲取父名和兒童在使用LINQ

public Class Category{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public ICollection<SubCategory> SubCategories {get;set;} 
} 

public Class SubCategory{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public int CategoryId { get; set; } 
    public virtual Category Category{ get; set; } 
    public ICollection<Ticket> Tickets { get; set; } 
} 

public class Ticket { 
    public Ticket(); 

    public int Id { get; set; } 
    public virtual SubCategory SubCategory{ get; set; } 
    public int SubCategoryId{ get; set; } 
} 

我想要得到的數據GROUPBY類別和使用此查詢取票的數量在每個子類:

Entities 
       .Include(h => h.SubCategories) 
       .ThenInclude(s => s.Tickets) 
       .GroupBy(s => s.Id) 
       .Select(t => new Cata { 
        Name = t.FirstOrDefault().Name, 
        Children = GetChildern(t.FirstOrDefault().SubCategories) 
       }); 



public List<SubCat> GetChildern(IEnumerable<SubCategories> subs) 
     { 
      var output = new List<SubCat>(); 
      foreach (var sub in subs) { 
       var subcat = new SubCat(); 
       subcat.Name = sub.Name; 
       if (sub.Tickets != null) { 
        subcat.Size = sub.Tickets.Count; 
       } 
       output.Add(subcat); 
      } 
      return output; 
     } 

使用上面的查詢,票據始終爲零,但存在票據。

+3

顯然,您的查詢屬於[Ignored Includes](https://docs.microsoft.com/en-us/ef/core/querying/related-data)類別。不要在沒有需要的情況下使用'GroupBy',並避免導致客戶端評估的自定義方法。 –

回答

2

我不明白爲什麼你需要的,如果你在@Ivan同意分類

var result= Entities 
      .Include(h => h.TicketSubCategories) 
      .ThenInclude(s => s.Tickets) 
      .Select(t => new Cata { 
          Name = t.Name, 
          Children= t.TicketSubCategories 
             .Select(ts=>new SubCat{ 
                Name=ts.Name, 
                Count=ts.Tickets.Count()}) 
         }; 

開始查詢做一組有關他上述評論,在這裏你不需要使用自定義方法,使用它將強制在客戶端執行查詢的投影,而不是在服務器上執行(您的數據庫)

+0

不,我已經更新了這個問題。 –

+0

在閱讀你的更新之後,我想我現在明白你的期望是什麼作爲輸出,看看我的第二個查詢 – octavioccl

+0

謝謝,這工作。 –

0

因此,每個類別都有零個或多個SubCategories,並且每個SubCategory都有零個或多個Tickets。每個票據都屬於一個子類別,每個子類別只屬於一個類別

而且您希望查詢產生具有相同類別的SubCategories組。您需要每個子類別的一些(或全部)屬性,但最重要的是,您需要每個子類別具有的門票數量。

每組子類中的所有元素都屬於同一個類別。您還需要此類別的一些(如果不是全部)屬性。

解決方案是將所有SubCategories分組到相同類別的組(爲了效率使用CategoryId)。然後使用Select來獲取你想要的屬性。

var result = SubCategories 
    // group them into groups with same CategoryId 
    .GroupBy(subCategory => subCategory.CategoryId 
    // from every group take the properties you want: 
    .Select(group => new 
    { 
     // All SubCategories in one group belong to the same Category. 
     // For efficiency, take only the Category properties you plan to use, 
     CommonCategory = group.Key.Select(category => new 
     { 
      // take the category properties you want to use 
     } 

     // The group has a lot of SubCategories. 
     // For each subcategory select only the properties you want to use 
     SubCategories = group.Select(subCategory => new 
     { 
      // one of the properties you want is the number of Tickets of this SubCategory: 
      TicketCount = subCategory.Tickets.Count(), 

      // for efficiency: select only SubCategory properties you plan to use: 
      Property1 = subCategory.Property1, 
      Property2 = subCategory.Property2, 
      ... 
     }), 
    }); 

所以結果是一系列對象。每個對象都有兩個屬性:

  • SubCategories:屬於同一個Category的所有SubCategories的一些屬性的序列。
  • CommonCategory。所有SubCategories所屬類別的幾個屬性。

SubCategories是一個序列。該序列中的每個元素是與幾個屬性的對象:

  • TicketCount:門票在子類別數
  • 其它性能的次類別

從這若干其他性質,很容易構建代碼爲GetChildren