Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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# 访问虚拟子对象执行sql而不使用WHERE子句_C#_Sql Server_Entity Framework_Linq To Sql - Fatal编程技术网

C# 访问虚拟子对象执行sql而不使用WHERE子句

C# 访问虚拟子对象执行sql而不使用WHERE子句,c#,sql-server,entity-framework,linq-to-sql,C#,Sql Server,Entity Framework,Linq To Sql,我试图研究为什么,我认为这应该是一个相对直截了当的概念,没有如预期的那样发挥作用。从我所读到的内容来看,以下内容应该是我所期望的,但事实并非如此 我正在使用实体框架从SQL数据库检索数据,但是在访问子对象时,我无法使延迟/延迟加载正常工作 这是我的班级 public class Product { [Key] public int Id { get; set; } public string Name { get; set; }

我试图研究为什么,我认为这应该是一个相对直截了当的概念,没有如预期的那样发挥作用。从我所读到的内容来看,以下内容应该是我所期望的,但事实并非如此

我正在使用实体框架从SQL数据库检索数据,但是在访问子对象时,我无法使延迟/延迟加载正常工作

这是我的班级

public class Product 
{
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string ShortDescription { get; set; }
        public string FullDescription { get; set; }
        public virtual IList<ProductEvent> Events { get; set; } = new List<ProductEvent>();
}
以及GetProduct方法:

    public Product GetProduct(int id, params Expression<Func<Product, object>>[] includedProperties)
    {
        IQueryable<Product> products = _db.Products;

        if (includedProperties != null)
        {
            foreach (var includeProperty in includedProperties)
            {
                products = products.Include(includeProperty);
            }
        }

        return products.Single(p => p.Id == id);
    }
我希望生成的SQL包含一个WHERE子句,该子句只返回具有匹配StartDateTime的事件,但是SQL不包含任何WHERE子句,因此所有产品事件都加载到内存中并在那里进行过滤。我的一些产品有超过100000个事件,因此这会导致性能问题

我无法理解在访问事件时导致我的过滤器被忽略的代码的错误

在EF处理查询的方式上,我是否遗漏了一些基本的东西?还是我访问产品的方式首先导致了问题

通过对产品调用
.Single()
,您已经执行了查询。它已加载到内存中,并且由于您的
.Include()
,该产品的子
事件也已加载到内存中。通过对您的产品调用
.Single()
,您已经执行了查询。它已加载到内存中,并且由于您的
.Include()
,该产品的子
事件也已加载到内存中。可能存在重复的
    public Product GetProduct(int id, params Expression<Func<Product, object>>[] includedProperties)
    {
        IQueryable<Product> products = _db.Products;

        if (includedProperties != null)
        {
            foreach (var includeProperty in includedProperties)
            {
                products = products.Include(includeProperty);
            }
        }

        return products.Single(p => p.Id == id);
    }
var desiredEvent = product.Events.SingleOrDefault(e => e.StartDateTime == '2017-07-01T02:00');