![](/img/trans.png)
[英]Filter Parent Collection Using Criteria in Child Collection via a Linq Query
[英]Linq Using Child Collection in Query
我有下面的實體,底部有查詢。 我沒有關注如何在返回集中包含聽證會的子實體集合(數字內容)。 如果我不使用包含,它的空白(不應該是),並且使用包含我收到以下錯誤消息:
$exception {"表達式 'h.digital_Contents' 在 'Include' 操作中無效,因為它不代表屬性訪問:'t => t.MyProperty'。要定位在派生類型上聲明的導航,請使用強制轉換 (' t => ((Derived)t).MyProperty') 或 'as' 運算符 ('t => (t as Derived).MyProperty')。可以通過組合 Where、OrderBy(Descending)、ThenBy 來過濾集合導航訪問(降序)、Skip 或 Take 操作。有關包含相關數據的詳細信息,請參閱http://go.microsoft.com/fwlink/?LinkID=746393。"} System.InvalidOperationException
public class Hearing
{
[Key]
public int Id { get; set; }
public int HearingNumber { get; set; }
public DateTime? End_Date { get; set; }
public ICollection<Digital_Content> digital_Contents = new List<Digital_Content>();
}
public class Digital_Content
{
[Key]
public int Id { get; set; }
[ForeignKey("Hearing")]
public int HearingId { get; set; }
public Hearing Hearing { get; set; }
public string BlobStorageLink { get; set; }
public DateTime? End_Date { get; set; }
}
詢問:
var ListHearings = await _context.Hearing
.Where(h => h.HearingNumber == 55)
.Include(h => h.digital_Contents)
.ToListAsync();
添加一個{ get; set; }
{ get; set; }
{ get; set; }
到您的digital_contents
以將其從字段更改為屬性,以便您的 digital_contents 可以被讀取和寫入(即填充您的屬性):
public ICollection<Digital_Content> digital_Contents { get; set; } = new List<Digital_Content>();
然后您的.Include(x => x.digital_Contents)
應該填充到您的返回集中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.