2017-03-18 63 views
1

你好我想包括兒童和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; } 
} 

基本上,SharePostAddress是一比一的關係。所以它們具有相同的主鍵值。然後,當我加載SharePost對象時,我想包括PlaceSuburb對象。所以我嘗試這樣

public SharePost GetFullSharePost(int postId) 
{ 
    return DataContext.SharePosts.Include(m => m.Address.Select((a => a.Suburb)) && (a => a.Place)) 
} 

Address.Select()不起作用。 Asp.net無法識別地址上的Select()方法。 Select()僅適用於List或ICollection?那麼我怎樣才能包括PlaceSuburb對象在Address當我加載Sharepost對象?

回答

2

如果要包含集合,則應該使用Select。試試這個:

return DataContext.SharePosts 
    .Include(x => x.Address.Suburb) 
    .Include(x => x.Address.Place); 
相關問題