使用include不会改变行为 [英] Using include doesn't change the behavior

查看:224
本文介绍了使用include不会改变行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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);






我在两种情况下都得到相同的结果,不知道区别 。
渴望加载需要使用 Include

解决方案

这两个查询都是通过使用 Eager Loading (并且是加载是通过使用 Include 方法来实现的),第二个查询使用 >这是默认情况。但是由于您的查询将仅返回 EmailAddresses ,因为 Select() SelectMany() 操作 Include()方法不会更改行为。在 c c>

b
$ b

要了解这两种加载相关实体之间的一些区别,当您需要主表的所有检索行的相关数据时,通常更有效地加载。而且,当关系 太多时,希望加载将是减少服务器上进一步查询的好习惯。但是当你知道你不会立即需要一个财产,那么懒惰加载也许是一个不错的选择。而且,在您的数据库环境将被处理并且延迟加载不再发生的情况下,也希望加载是一个很好的选择。
为了证明一个是Lazy Loading,一个是Eager Loading,请考虑以下代码:

  public List< Person> GetEmailAddresses()
{
using(yourEntities awlt = new yourEntities())
{
var query = awlt.People
.Where(p => p。 LastName.Equals(lastName的));
return query.ToList();
}
}

调用此方法后,无法加载相关实体懒惰,因为db被处置。要证明这样做:

  var query = GetEmailAddresses(); 
foreach(查询中的var项目.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1))
{
MessageBox.Show(item);
}

您将收到此错误:


ObjectContext实例已被处理,不能再用于需要连接的操作。


但是如果您更改 GetEmailAddresses 以使用Eager Loading这样:

  public List< Person> GetEmailAddresses()
{
使用(yourEntities awlt = new yourEntities())
{
var query = awlt.People.Include(EmailAddresses)
.Where (p => p.LastName.Equals(lastName));
return query.ToList();
}
}

然后下面的代码应该可以正常工作:

  var query = GetEmailAddresses(); 
foreach(查询中的var项目.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1))
{
MessageBox.Show(item);
}

所以在你的数据库上下文处理的情况下,加载将是一个更好的选择。


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);


I get the same results in both cases without knowing the difference . Does the Eager Loading require using 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();
    }
}

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);                
}

And you will get this error:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

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);                
}

So in a situation where your db context would be disposed the Eager Loading would be a better choice.

这篇关于使用include不会改变行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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