Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Lambda如何在dbcontext中同时获取包含_C#_Lambda - Fatal编程技术网

C# Lambda如何在dbcontext中同时获取包含

C# Lambda如何在dbcontext中同时获取包含,c#,lambda,C#,Lambda,我在dbcontext中有4个类,它是EventRemind.cs Event.cs House.cs Customer.cs,代码如下: public class EventRemind { [Key] public Guid Id { get; set; } public Guid CustomerEventId { get; set; } public DateTime RemindTime { get; set; } public bool

我在dbcontext中有4个类,它是EventRemind.cs Event.cs House.cs Customer.cs,代码如下:

public class EventRemind
{
    [Key]
    public Guid Id { get; set; }

    public Guid CustomerEventId { get; set; }

    public DateTime RemindTime { get; set; }

    public bool HasRead { get; set; }

    public DateTime CreatedTime { get; set; }

    [ForeignKey("CustomerEventId")]
    public virtual Event CustomerEvent { get; set; }
}

public class Event
{
    [Key]
    public Guid Id { get; set; }

    public string Title { get; set; }

    public int UserId { get; set; }

    public int HouseId { get; set; }

    [MaxLength(800)]
    public string Content { get; set; }

    public DateTime CreatedTime { get; set; }

    [ForeignKey("HouseId")]
    public virtual House House { get; set; }

    [ForeignKey("UserId")]
    public virtual Customer Customer { get; set; }

    public virtual ICollection<EventRemind> EventReminds { get; set; }
}

public class House
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Event> HouseEvents { get; set; }
}

public class Customer
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Event> CustomerEvents { get; set; }
}

为什么会这样?有人能帮我吗?提前谢谢。

我想你的最后一个操作员应该是
包括
。试试这个:

var query = _dataContext.EventReminds
            .Include(c => c.CustomerEvent)
            .ThenInclude(c => c.Customer)
            .Include(c => c.House);

您必须按照以下方式编写代码

途径1

方式2(如果属性不是集合,则它是有用的)


你能试着把你的代码简化成一个简单的代码吗?
var query = _dataContext.EventReminds.Include(c => c.CustomerEvent)
            .ThenInclude(c => c.Customer).ThenInclude(c => c.House //this get a compile error);
var query = _dataContext.EventReminds
            .Include(c => c.CustomerEvent)
            .ThenInclude(c => c.Customer)
            .Include(c => c.House);
var query = 
_dataContext.EventReminds.Include(c => c.CustomerEvent).ThenInclude(c => c.Customer)
                         .Include(c=> c.CustomerEvent).ThenInclude(c => c.House);
var query = 
_dataContext.EventReminds.Include(c=> c.CustomerEvent).
                     .Include(c=> c.CustomerEvent.Customer)
                     .Include(c=> c.CustomerEvent.House);