简体   繁体   English

ObjectContext.ExecuteStoreQuery是否迭代结果集?

[英]Does ObjectContext.ExecuteStoreQuery iterate over the result set?

I am using ObjectContext.ExecuteStoreQuery to return a list of entities, about 30K. 我使用ObjectContext.ExecuteStoreQuery返回实体列表,大约30K。

I need to improve performance so I will be "selecting" 4 properties that I really need, instead of the total 20 properties. 我需要提高性能,所以我将“选择”我真正需要的4个属性,而不是总共20个属性。

I could create a DTO with these 4 properties and pass it to ObjectContext.ExecuteStoreQuery, but I would prefer to use a DataReader, iterate over the 30K, and build my list. 我可以使用这4个属性创建一个DTO并将其传递给ObjectContext.ExecuteStoreQuery,但我更喜欢使用DataReader,迭代30K,并构建我的列表。

I tried both options and it "seems" they take the same time (did not measure it). 我尝试了两种选择,它“似乎”它们需要同时(没有测量它)。 My question is -does ObjectContext.ExecuteStoreQuery iterate over the result set, as I am doing manually with a DataReader? 我的问题是-does ObjectContext.ExecuteStoreQuery迭代结果集,因为我正在用DataReader手动完成?

I found this in MSDN, but does not really answer my question. 我在MSDN中找到了这个,但并没有真正回答我的问题。 Thank you. 谢谢。

Calling the ExecuteStoreQuery method is equivalent to calling the ExecuteReader method of the DbCommand class, only ExecuteStoreQuery returns entities and the ExecuteReader returns property values in the DbDataReader. 调用ExecuteStoreQuery方法相当于调用DbCommand类的ExecuteReader方法,只有ExecuteStoreQuery返回实体,而ExecuteReader返回DbDataReader中的属性值。

If you mean, does it hit the database on each iteration, then (going by the documentation) no it will load the entire resultset into your entity. 如果您的意思是,它是否在每次迭代时都访问数据库,那么(通过文档)不会将整个结果集加载到您的实体中。 Once loaded, it will iterate through the stored data in memory, though 一旦加载,它将迭代存储器中的存储数据

ExecuteStoreQuery returns a System.Data.Objects.ObjectResult and it query directly the database using TSQL, you won't have a stream but just a collection of all the objects queried. ExecuteStoreQuery返回一个System.Data.Objects.ObjectResult ,它使用TSQL直接查询数据库,你不会有一个流,而只是一个查询过的所有对象的集合。

http://blogs.msdn.com/b/alexj/archive/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-database.aspx http://blogs.msdn.com/b/alexj/archive/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-database.aspx

If I understand correctly this is not what you want. 如果我理解正确,这不是你想要的。

If you want to query data in some sort of way similar to a classic ADO.NET DataReader you should query directly the EntityClient , it is different from using Linq To Entities or ESQL because you do not pass through the ObjectContext, and the query will not materializes any object! 如果您想以某种类似于经典ADO.NET DataReader的方式查询数据,您应该直接查询EntityClient ,它与使用Linq To Entities或ESQL不同,因为您没有通过ObjectContext,并且查询不会实现任何对象!

using (EntityConnection conn = new EntityConnection("name=SampleEntities"))
{
    conn.Open();
    EntityCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT VALUE c FROM SampleEntities.Contacts AS c WHERE c.FirstName='Robert'";
    using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection))
    {
        while (rdr.Read())
        {
            var firstname = rdr.GetString(1);
            var lastname = rdr.GetString(2);
        }
    }
    conn.Close();
}

It is helpful if you need just to query some read-only data you need to display in some way, it is no different from creating any other provider command and setting its CommandText, but The CommandText here is the Entity SQL expression and you obviously query the EDM that i think it is why you are using EF. 如果您只需要以某种方式查询需要显示的某些只读数据,这有助于创建任何其他提供程序命令并设置其CommandText,但这里的CommandText是Entity SQL表达式,您显然在查询EDM,我认为这就是你使用EF的原因。

Hope this helps 希望这可以帮助

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

相关问题 实体框架:ObjectContext.ExecuteStoreQuery产生分离的对象 - Entity Framework: ObjectContext.ExecuteStoreQuery produces detached objects EF6:ObjectContext.ExecuteStoreQuery返回空列表 - EF6: ObjectContext.ExecuteStoreQuery returns empty List 使用ObjectContext.ExecuteStoreQuery在SQL查询中参数化“ ORDER BY” <T> () - Parameterizing 'ORDER BY ' in SQL query with ObjectContext.ExecuteStoreQuery<T>() 每个结果集的ObjectContext是否都会更改? - Does the ObjectContext change for each result set? 实体框架:在ObjectContext中找不到ExecuteStoreQuery - Entity Framework : ExecuteStoreQuery not found in ObjectContext 每组操作都可重用的ObjectContext或新的ObjectContext? - Reuseable ObjectContext or new ObjectContext for each set of operations? WPF BubbleSeries,遍历气泡并设置样式 - WPF BubbleSeries, iterate over the bubbles and set the style AdaBoost算法会迭代什么? - What does the AdaBoost algorithm iterate over? dbContext.ExecuteStoreQuery <t> 在dbcontext上不存储对象? - dbContext.ExecuteStoreQuery<t> does not store objects on dbcontext? 如何迭代这种特殊类型的Dictionary <int, Result> ? - How to iterate over this particular type of Dictionary<int, Result>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM