【问题标题】:Includes child and child's objects when the child object is not list or collection当子对象不是列表或集合时,包括子对象和子对象
【发布时间】: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; }
}

基本上,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?那么当我加载Sharepost 对象时,如何在Address 中包含PlaceSuburb 对象?

【问题讨论】:

    标签: c# .net entity-framework linq


    【解决方案1】:

    当你想包含一个集合时,你应该使用Select。试试这个:

    return DataContext.SharePosts
        .Include(x => x.Address.Suburb)
        .Include(x => x.Address.Place);
    

    【讨论】:

      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多