2017-03-18 57 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); 
相关问题