简体   繁体   中英

Includes child and child's objects when the child object is not list or collection

Hi I am trying to include child and child's object by linq

here is my parent class

public class SharePost : BasePost
{
    public Address Address { get; set; }

    public ICollection<Picture> Pictures { get; set; }
}

here is my child class which is "Address"

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; }
}

Basically, SharePost and Address are one-to-one relationship. so they have the same primary key value. Then I want to include Place and Suburb object when I load SharePost Object. So I tried like this

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

But the Address.Select() does not work. The Asp.net does not recognize the Select() method on Address. Does the Select() only work with List or ICollection ? Then How can I include the Place and Suburb object in Address when I load Sharepost Object?

You should use Select when you want to include a collection. Try this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM