[英]Includes child and child's objects when the child object is not list or collection
嗨,我正在嘗試通過linq包含子對象
這是我的父母班
public class SharePost : BasePost
{
public Address Address { get; set; }
public ICollection<Picture> Pictures { get; set; }
}
這是我的孩子班,是“地址”
public class Address
{
public SharePost SharePost { get; set; }
public int PostId { get; set; }
public Place Place { get; set; }
public int PlaceId { get; set; }
public Suburb Suburb { get; set; }
public int SuburbId { get; set; }
}
基本上, SharePost
和Address
是一對一的關系。 因此它們具有相同的主鍵值。 然后,當我加載SharePost對象時,我想包括Place
和Suburb
對象。 所以我嘗試了這樣
public SharePost GetFullSharePost(int postId)
{
return DataContext.SharePosts.Include(m => m.Address.Select((a => a.Suburb)) && (a => a.Place))
}
但是Address.Select()
不起作用。 Asp.net無法識別Address上的Select()
方法。 Select()
僅適用於List或ICollection
嗎? 然后,在加載Sharepost
對象時如何在Address
包含Place
和Suburb
對象?
要包含集合時,應使用“ Select
。 嘗試以下方法:
return DataContext.SharePosts
.Include(x => x.Address.Suburb)
.Include(x => x.Address.Place);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.