[英]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.