循环通过IQueryable与循环通过Datareader与Reader.Read()在C# [英] Is looping through IQueryable the same as looping through a Datareader with Reader.Read() in C#

查看:233
本文介绍了循环通过IQueryable与循环通过Datareader与Reader.Read()在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我找不到任何答案,这个问题已经在我头脑中盘旋了一段时间了。



基本上我想知道,在将结果集映射到其中一个实体/ sql之前,实体/ sql的linq是否循环通过datareader类或它已经映射。在我可以循环访问Iqueryable之前,Iqueryable必须循环访问数据读取器。为了显示我在代码中的含义,我想知道如果

  foreach(SomeIQueryableObject中的对象o)
{
...使用对象
}

将导致A:

  while(Reader.Read())
{
SomeIqueryableObject.Add(Reader.TranslateRow() );
}
//现在,iqueryable已经加载了对象,我们可以循环使用
//与上述代码
foreach(ObjectI in SomeIQueryableObject)
{
...用对象
}

导致B:

  while(Reader.Read())
{
...做东西对象
}
// Iqueryable不必循环通过Reader
//,然后才能循环遍历Iqueryable。

简短摘要:当我使用foreach on iqueryable时,这等价于循环通过datareader与Reader.Read()

解决方案

AFAIK EF将使用您的第二个选项。我说的这个原因是我经历过,当循环执行第一个查询的结果时,在执行另一个查询时,EF会打开第二个连接。由于我正在使用一个事务,而且我禁用了分布式事务,另一个打开的连接(当第一个连接仍然打开并被使用时)禁用了当前事务的重用,我的第二个查询引发了一个异常,指出DTC wasn 't enabled。



所以我的结论是:如果你正在执行查询并循环结果(所以没有 .ToList()),则查询仍在执行,连接仍然处于打开状态,如果EF正在使用数据读取器,那么是的,它在从数据读取器读取的循环中。 >

Sorry for the long title :)

I couldn't really find any answers this and this question has been circling through my head for some time now. Short summary of question at the end.

Basically I'm wondering if linq to entities/sql loops through a datareader before it maps the resultset to one of the entities/sql classes or is it already mapped. Does the Iqueryable have to loop through a data reader before I can loop through the Iqueryable. To show what I mean in code I'm wondering if

foreach(object o in SomeIQueryableObject)
{
    ...do stuff with the object
}

will result in either A:

 while(Reader.Read())
{
     SomeIqueryableObject.Add(Reader.TranslateRow());
}
//now the iqueryable has been loaded with objects and we can loop through it
//with the above code
foreach(object o in SomeIQueryableObject)
{
    ...do stuff with the object
}

or will it result in B:

while(Reader.Read())
{
    ...do stuff with the object
}
//Iqueryable doesn't have to loop through Reader
//before you can loop through Iqueryable.

Short summary: when I use a foreach on an iqueryable, is this the equivalent of looping through a datareader with Reader.Read()

解决方案

AFAIK EF will use your second option. The reason for me saying this is that I have experienced that EF will open a second connection when you execute another query while looping through the results of the first query. Since I was using a transaction, AND I disabled distributed transaction, and a second open connection (while the first connection is still open and is used) disables the re-use of the current transaction, my second query threw an exception saying that DTC wasn't enabled.

So my conclusion was: if you are executing a query and looping through the results (so no .ToList()), then the query is still executing, and the connection is still open, and if EF is using a data-reader, then yes, it is within the loop which reads from the data-reader.

这篇关于循环通过IQueryable与循环通过Datareader与Reader.Read()在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆