Skip to main content
 首页 » 编程设计

c#之将执行以下哪些 linq 查询

2024年02月23日14lori

我如何知道将执行以下哪些 linq 查询?

    // I think this will not be executed 
    var temp1 = PdvEntities.Entities.Products; 
 
    // Not sure 
    IEnumerable<Data.Products> temp2 = PdvEntities.Entities.Products; 
 
    // will not be executed 
    var temp3 = from a in PdvEntities.Entities.Products select a; 
 
    ListView lv1 = new ListView(); 
    lv1.DataContext = temp1; // will this make the first query to be executed? 
    lv1.ItemsSource = temp2; // will this make the second query execute? 
 
    // I think this will be executed 
    var temp4 = PdvEntities.Entities.Products.ToList(); 

请注意,我正在使用 ADO.Net 实体数据模型从我的数据库中的表 Products 执行查询

请您参考如下方法:

这个概念称为“延迟执行”,一般的经验法则是在您实际访问基础数据之前不会执行查询。另一种思考方式是,当您不再返回时 IEnumerable或 IQueryable ,您可能正在执行代码,就像您的最后一个 temp4 正在放入一个 List<T>这肯定是在访问数据。

下面是一个不错的解释:

http://www.codeguru.com/columns/vb/article.php/c16935