【发布时间】:2017-03-18 11:44:51
【问题描述】:
嗨,我正在尝试通过 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 无法识别地址上的Select() 方法。 Select() 是否仅适用于 List 或 ICollection?那么当我加载Sharepost 对象时,如何在Address 中包含Place 和Suburb 对象?
【问题讨论】:
标签: c# .net entity-framework linq