简体   繁体   English

EF Linq包含嵌套数组

[英]EF Linq include nested arrays

I have 3 classes in EF-Code-First 我在EF代码优先课程中有3个课程

public class User {
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<Flat> Flats { get; set; }
}

public class Flat {
    public string Id { get; set; }
    public string Number { get; set; }
    public virtual House House { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

public class House {
    public string Id { get; set; }
    public string Street { get; set; }
    public string Number { get; set; }
    public virtual ICollection<Flat> Flats { get; set; }
}

And I want to get all users. 我想吸引所有用户。

I do: 我做:

var Result = _storage.Users
    .Include(x => x.Flats)
    .Include(y => y.Flats.Select(z => z.House))
    .ToList();

And in Result I have Users with Flats , but Flats don't have their Houses . 而在结果我有平底鞋 的用户 ,但平底鞋没有自己的房子

What am I doing wrong? 我究竟做错了什么?

You only need to add one include, the one to the deepest level: 您只需要添加一个包含,一个包含到最深层次:

var Result = _storage.Users
.Include(user => user.Flats.Select(flat => flat.Houses))
.ToList();

This will automatically include all Houses properties as well as all Flat properties and the Houses properties 这将自动包括所有房屋属性以及所有公寓属性和房屋属性

Other Flat Collections will not be included. 其他平面集合将不包括在内。 For those you would need an extra Include. 对于那些您将需要一个额外的包含。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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