实体框架首先选择没有.ToList()的新POCO [英] Entity Framework Select new POCO without .ToList() first

查看:112
本文介绍了实体框架首先选择没有.ToList()的新POCO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含服务层(WCF网站)和Silverlight 4客户端的应用程序。 RIA服务不是一个选择,所以我们创建中介类来传递。为了这个问题的目的,我们假设我是来回传递美味的食物对象。

I'm creating an application with a service layer (WCF Website) and a Silverlight 4 Client. RIA Services are not an option, so we create intermediary classes to pass back and forth. For the purpose of this question let's assume I'm passing back and forth Tasty Food Objects.

public class FoodData
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Tastyness TastyLevel { get; set; }
}

EF模型基本上是同一个类,一个有三个基本字段的表(Tastyness是一个对应于我们的枚举Tastyness的int)。

The EF Model is essentially the same class, a table with three basic fields (the Tastyness is an int that corresponds to our enum Tastyness).

在执行Entity Framework查询时,我发现自己使用这种语句很多:

I find myself using this kind of statement a lot when doing Entity Framework queries:

public List<FoodData> GetDeliciousFoods()
{
  var deliciousFoods = entities.Foods
                               .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                               .ToList()  // Necessary? And if so, best performance with List, Array, other?
                               .Select(dFood => dFood.ToFoodData())
                               .ToList();

  return deliciousFoods;
}

没有.ToList()调用我收到一个关于LINQ无法关闭的异常将自定义方法翻译成等效的查询,我明白了。

Without the .ToList() call I get an exception about LINQ not being able to translate the custom method to a query equivalent, which I understand.

我的问题是关于在 .ToList()之前调用。使用自定义扩展名选择(...),将我们的对象转换为Food对象的POCO版本。

My question is about the call to .ToList() before the .Select(...) with the custom extension to convert our object to the POCO version of the Food object.

有没有更好的模式在这里做,或者甚至可能更好的替代.ToList()可能会更加性能,因为我不需要功能列表< ..>结果。

Is there a better pattern to do here, or maybe even a better alternative to .ToList() that may be more performant since I don't really require the functionality of the List<..> result.

推荐答案

使用 ToList AsEnumerable 是您实现整个实体并支付修复费用。如果你想有最好的SQL返回只需要的字段,那么你应该直接投入,而不是使用 .ToFoodData()

The problem with using ToList or AsEnumerable is that you materialize the entire entity and pay the cost of fixup. If you want to have the best possible SQL which returns only the needed fields, then you should project directly rather than using .ToFoodData():

var deliciousFoods = entities.Foods
                             .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                             .Select(dFood => new FoodData
                                  {
                                      Id = dFood.Id,
                                      Name = dFood.Name,
                                      TastyLevel = (Tastyness)dFood.Tastyness
                                  });

转换为枚举可能是一个问题。如果是,请通过匿名类型:

The cast to enum may be a problem. If so, go through an anonymous type:

var deliciousFoods = entities.Foods
                             .Where(f => f.Tastyness == (int)Tastyness.Delicious)
                             .Select(dFood => new FoodData
                                  {
                                      Id = dFood.Id,
                                      Name = dFood.Name,
                                      TastyLevel = dFood.Tastyness
                                  })
                             .AsEnumerable()
                             .Select(dFood => new FoodData
                                  {
                                      Id = dFood.Id,
                                      Name = dFood.Name,
                                      TastyLevel = (Tastyness)dFood.TastyLevel
                                  });

如果您检查生成的SQL,您会看到它更简单,而您不支付将对象固定到ObjectContext中的成本。

If you examine the resulting SQL, you'll see it's simpler, and you don't pay the cost of fixing up objects into the ObjectContext.

这篇关于实体框架首先选择没有.ToList()的新POCO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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