簡體   English   中英

排除實體框架核心的Count函數中的派生類

[英]Excluding the derived classes in the Count funtion in entity framework core

我有以下聲明。

 List<ApplicationUserDto> peers = _context.ApplicationUsers
        .Select(m => new ApplicationUserDto
          {
              Id = m.Id,
              MyCount = m.GroupMemberships
                         .Count(pg => pg.StudentGroup.ReviewRoundId == reviewRoundId)
          }).ToList();

我還有另一個 class,名為PeerStudentGroup派生自StudentGroup Count() function 中,我不希望它們被包括在內。 我的意思是我只想計算它是StudentGroup類型(而不是從它派生的另一個 class)。 我想知道我怎么能做到這一點。 有什么建議么?

在這種情況下,您可以使用比較實例類型的is關鍵字。 您應該將.(pg.StudentGroup is PeerStudentGroup)添加到您的條件中。

您的代碼應如下所示:

 List<ApplicationUserDto> peers = _context.ApplicationUsers
        .Select(m => new ApplicationUserDto
          {
              Id = m.Id,
              MyCount = m.GroupMemberships
                         .Count(pg => pg.StudentGroup.ReviewRoundId == reviewRoundId && !(pg.StudentGroup is PeerStudentGroup))
          }).ToList();

這可能有助於開始:

在投影(Select)之前應用a.Where(過濾)語句,如

MyCount = m.GroupMemberships.Where(gm =>.(gm is PeerStudentGroup)) [alternatively] typeof(gm).= typeof(PeerStudentGroup).Count(pg => pg.StudentGroup.ReviewRoundId == reviewRoundId)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM