Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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# 实体框架虚拟ICollection如何查询_C#_Entity Framework - Fatal编程技术网

C# 实体框架虚拟ICollection如何查询

C# 实体框架虚拟ICollection如何查询,c#,entity-framework,C#,Entity Framework,首先,我不熟悉实体框架等,并试图弄明白一些事情。我有一个这样的模型: public class EventInstance { [Column("EVENT_INSTANCE_ID")] public int EventInstanceID { get; set; } [Column("CUSTOMER_ID")] public int CustomerID { get; set; } [Column("EVENT_ID")] public

首先,我不熟悉实体框架等,并试图弄明白一些事情。我有一个这样的模型:

 public class EventInstance {

    [Column("EVENT_INSTANCE_ID")]
    public int EventInstanceID { get; set; }

    [Column("CUSTOMER_ID")]
    public int CustomerID { get; set; }

    [Column("EVENT_ID")]
    public int EventID { get; set; }

    [Column("START_DATE_TIME")]
    public System.DateTime StartDateTime { get; set; }

    public virtual Event Event { get; set; }

}
 public IQueryable<EventInstance> GetInstances(int scheduleID) {
            // only returning instances that are 3 months back
            DateTime dateRange = DateTime.Now.AddDays(-180);
            return EventDBContext.EventInstances.Where
                (x => x.CustomerID == MultiTenantID && x.StartDateTime >= dateRange)
                .OrderBy(x => x.StartDateTime).AsQueryable();
        }
我需要访问名为EventTimeEventInstances的表中的属性,但该表不包括在模型中。我有两个问题

如果我加上:

public virtual ICollection<EventTimeEventInstance> EventTimeInstances { get; set; }
公共虚拟ICollection EventTimeInstances{get;set;}
这会影响我们应用程序的其他方面吗

其次,如何在如下查询中从ICollection访问属性:

 public class EventInstance {

    [Column("EVENT_INSTANCE_ID")]
    public int EventInstanceID { get; set; }

    [Column("CUSTOMER_ID")]
    public int CustomerID { get; set; }

    [Column("EVENT_ID")]
    public int EventID { get; set; }

    [Column("START_DATE_TIME")]
    public System.DateTime StartDateTime { get; set; }

    public virtual Event Event { get; set; }

}
 public IQueryable<EventInstance> GetInstances(int scheduleID) {
            // only returning instances that are 3 months back
            DateTime dateRange = DateTime.Now.AddDays(-180);
            return EventDBContext.EventInstances.Where
                (x => x.CustomerID == MultiTenantID && x.StartDateTime >= dateRange)
                .OrderBy(x => x.StartDateTime).AsQueryable();
        }
publicIQueryable GetInstances(int scheduleID){
//仅返回3个月前的实例
DateTime dateRange=DateTime.Now.AddDays(-180);
返回EventDBContext.EventInstances.Where
(x=>x.CustomerID==MultiTenantID&&x.StartDateTime>=dateRange)
.OrderBy(x=>x.StartDateTime).AsQueryable();
}

我需要能够将EventTimeInstances.EventTimeID==scheudleID添加到此查询。我如何才能做到这一点?

您可以在查询中这样使用它:

public IQueryable<EventInstance> GetInstances(int scheduleID) 
{
    // only returning instances that are 3 months back
    DateTime dateRange = DateTime.Now.AddDays(-180);
    return EventDBContext.EventInstances.Where(x => 
            x.CustomerID == MultiTenantID && 
            x.StartDateTime >= dateRange && 
            x.EventTimeInstances.Any(a => a.EventTimeID == scheudleID) ).OrderBy(x => x.StartDateTime).AsQueryable();
}
publicIQueryable GetInstances(int scheduleID)
{
//仅返回3个月前的实例
DateTime dateRange=DateTime.Now.AddDays(-180);
返回EventDBContext.EventInstances.Where(x=>
x、 CustomerID==MultiTenantID&&
x、 StartDateTime>=日期范围&&
x、 EventTimeInstances.Any(a=>a.EventTimeID==scheudleID)).OrderBy(x=>x.StartDateTime).AsQueryable();
}