实体框架是否无法在查询中使用非实体类?

发布于 2024-10-06 04:26:48 字数 807 浏览 0 评论 0原文

我试图确定我是否做错了什么,或者实体框架是否不应该这样做。我想执行 LINQ 样式查询来填充对象,包括子元素列表。在 Linq2SQL 中,这是有效的,并且会转换为高效的 SQL 查询,其结果将填充新列表。

class ReportItem
{
    string Manager; /* ... */ 
    List<string> Employees; /* ... */
}

var report = (from manager in entities.Managers
                select new ReportItem()
                {
                    Manager = manager.Name,
                    Employees = manager.Employees.Select(e => e.Name).ToList()
                }).ToList();

使用实体框架,Employees 行将导致方法无法转换为存储表达式 错误 - 实体框架似乎不喜欢在 LINQ 语句内构造非实体类,尽管它确实这样做不涉及任何数据库端逻辑。

我知道使用实体类进行更改跟踪,但是是否无法将信息读取到任意类中?我不想为每个可能的读取场景构建实体和数据库视图,并且我不想通过管道发送一半的数据库(整个员工或经理实体)只是为了读取名称属性之类的内容。

是否可以使用实体框架执行这种“懒惰但高效”的查询,或者实体框架只是没有考虑到它?在将项目从 Linq2SQL 转换为实体框架时,我已经遇到了许多问题,这可能真的会毁掉这笔交易。

I am trying to determine if I am doing something wrong, or if the Entity Framework just isn't meant to do this. I want to perform a LINQ style query to populate an object, including lists of child elements. In Linq2SQL this is valid, and is converted into an efficient SQL query whose results populate the new list.

class ReportItem
{
    string Manager; /* ... */ 
    List<string> Employees; /* ... */
}

var report = (from manager in entities.Managers
                select new ReportItem()
                {
                    Manager = manager.Name,
                    Employees = manager.Employees.Select(e => e.Name).ToList()
                }).ToList();

With the Entity Framework the Employees line will result in a method cannot be translated into a store expression error - the Entity Framework does not seem to like constructing non-entity classes inside a LINQ statement, even though it does not involve any database side logic.

I understand that for change tracking entity classes are used, but is it not possible to read information into arbitrary classes? I don't want to construct entities and database views for every possible read scenario, and I don't want to send half the database across the pipe (entire Employee or Manager entities) just to read off something like the name property.

Is it possible to do this kind of 'lazy but efficient' query with the Entity Framework, or is the Entity Framework just not built with it in mind? I have already come across a number of issues converting a project from Linq2SQL to the Entity Framework, and this could really kill the deal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2024-10-13 04:26:48

LINQ to Entities 仅支持无参数构造函数和初始值设定项。

更具体地说,根据设计,LINQ to Entities 要求将整个 LINQ 查询表达式转换为服务器查询。在翻译查询之前,仅​​在客户端上计算一些不相关的子表达式(查询中不依赖于服务器结果的表达式)。 支持没有已知转换的任意方法调用,以及您的情况下的参数化初始值设定项。

也就是说,您正在寻找的内容可以通过匿名类型投影轻松完成:

var report = (from manager in entities.Managers
              select new 
              {
                  Manager = manager.Name,
                  Employees = manager.Employees.Select(e => e.Name)
              })
              .ToList();

LINQ to Entities only supports parameterless constructors and Initializers.

To be more specific, by design, LINQ to Entities requires the whole LINQ query expression to be translated to a server query. Only a few uncorrelated subexpressions (expressions in the query that do not depend on the results from the server) are evaluated on the client before the query is translated. Arbitrary method invocations that do not have a known translation, and parameterized initializers in your case, are not supported.

That said, what you are looking for could be easily accomplished by a Anonymous type Projection:

var report = (from manager in entities.Managers
              select new 
              {
                  Manager = manager.Name,
                  Employees = manager.Employees.Select(e => e.Name)
              })
              .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文