问题描述
有人可以帮我澄清一下:
Could Someone help me to clarify the difference between :
var query = awlt.People.Include(p => p.EmailAddresses)
.Where(p => p.LastName.Equals(lastName))
.SelectMany(a => a.EmailAddresses)
.Select(a => a.EmailAddress1);
<小时>
var query = awlt.People
.Where(p => p.LastName.Equals(lastName))
.SelectMany(a => a.EmailAddresses)
.Select(a => a.EmailAddress1);
<小时>
我在这两种情况下都得到了相同的结果,但不知道有什么区别.Eager Loading 是否需要使用 Include
?
推荐答案
这两个查询都是检索相关数据,只是第一次使用 Eager Loading(是的 Eager loading 是通过使用 Include
您猜到的方法)和第二个查询使用 <默认情况下,strong>延迟加载.但是由于您的查询将只返回 EmailAddresses
因为 Select()
和 SelectMany()
操作 Include()
方法不会改变行为.要查看 when Include()
方法在您的示例中很重要,请阅读以下几行,我将在一个示例中证明这一点:
The both query are retrieving the related data just first query by using Eager Loading (and yes Eager loading is achieved by use of the Include
method as you guessed) and the second query by using Lazy loading which is by default. But since your query will only returns EmailAddresses
because of the Select()
and SelectMany()
operations the Include()
method doesn't change the behavior. To see when Include()
method is matter in your example read the following lines that I will prove it in one example:
要了解这两种加载相关实体之间的一些区别,急切加载通常在您需要主表的所有检索行的相关数据时更有效.此外,当关系 不 太多时,急切加载 将是减少服务器上进一步查询的好习惯.但是当你知道你不会立即需要一个属性时,延迟加载可能是一个不错的选择.在您的数据库上下文将被处理并且不再发生延迟加载的情况下,急切加载也是一个不错的选择.为了证明一种是延迟加载,一种是急切,请考虑以下代码:
To know some difference between this two kind of loading related entities Eager loading is typically more efficient when you need the related data for all retrieved rows of the primary table. And also when relations are not too much, eager loading will be good practice to reduce further queries on server. But when you know that you will not need a property instantly then lazy loading maybe a good choice. And also eager loading is a good choice in a situation where your db context would be disposed and lazy loading could not take place anymore. To prove that one is Lazy Loading and one is Eager Loading consider the following code:
public List<Person> GetEmailAddresses()
{
using (yourEntities awlt = new yourEntities())
{
var query = awlt.People
.Where(p => p.LastName.Equals(lastName));
return query.ToList();
}
}
调用该方法后,无法懒加载相关实体,因为db被释放了.为了证明这一点:
After calling this method, You cannot load the related entity lazily because the db is disposed. To prove try this:
var query = GetEmailAddresses();
foreach (var item in query.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1))
{
MessageBox.Show(item);
}
你会得到这个错误:
ObjectContext 实例已被释放,不能再用于需要连接的操作.
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
但是,如果您将 GetEmailAddresses
更改为像这样使用 Eager Loading:
But if you change the GetEmailAddresses
to use Eager Loading like this:
public List<Person> GetEmailAddresses()
{
using (yourEntities awlt = new yourEntities())
{
var query = awlt.People.Include("EmailAddresses")
.Where(p => p.LastName.Equals(lastName));
return query.ToList();
}
}
那么下面的代码应该可以正常工作:
Then the below code should works fine:
var query = GetEmailAddresses();
foreach (var item in query.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1))
{
MessageBox.Show(item);
}
因此,在您的数据库上下文将被处置的情况下,Eager Loading 将是更好的选择.
So in a situation where your db context would be disposed the Eager Loading would be a better choice.
这篇关于使用 include 不会改变行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!