实体框架首先选择没有.ToList()的新POCO

编程入门 行业动态 更新时间:2024-10-26 07:30:25
本文介绍了实体框架首先选择没有.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

本文发布于:2023-11-15 12:29:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1594452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   POCO   ToList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!