实体框架限制返回的 nvarchar 列的长度

发布于 2024-10-29 02:34:27 字数 800 浏览 1 评论 0原文

我想限制 EF 查询中列的长度,唉:

 var query = from ce in entities.ContactEvents
              .Include("Person")
              .Include("Orders")
             where ce.PersonID = personID
             orderby ce.DateTimeContact descending
             select new ContactEvent 
             {
                  ID = ce.ID,
                  DateTimeContact = ce.DateTimeContact,
                  Description = ce.Description.Substring(0, 500),
                  Orders = ce.Orders
             };

查询失败,因为 EF 无法投影复杂类型 Orders。

无法在 LINQ to Entities 查询中构造实体或复杂类型“Model.ContactEvent”。

我尝试了几种不同的方法来完成相同的操作,例如在 LINQ 表达式中使用显式联接,但到目前为止,我总是在选择投影中填充 Orders 集合时遇到障碍。

关于如何构建查询有什么想法吗?理想情况下,我什至不想使用选择投影,但我假设我需要这样做,以便能够限制从数据库返回的描述列的长度。

I want to limit the length of a column in an EF query, ala:

 var query = from ce in entities.ContactEvents
              .Include("Person")
              .Include("Orders")
             where ce.PersonID = personID
             orderby ce.DateTimeContact descending
             select new ContactEvent 
             {
                  ID = ce.ID,
                  DateTimeContact = ce.DateTimeContact,
                  Description = ce.Description.Substring(0, 500),
                  Orders = ce.Orders
             };

The query fails because the EF can't project the complex type Orders.

The entity or complex type 'Model.ContactEvent' cannot be constructed in a LINQ to Entities query.

I've tried a few different ways to do the same thing such as use an explicit join in the LINQ expression but so far I always hit a snag populating the Orders collection in the select projection.

Any ideas on how I can construct my query? Ideally I don't even want to use a select projection but I'm assuming I need to in order to be able to limit the length of the description column returned from the database.

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

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

发布评论

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

评论(1

守望孤独 2024-11-05 02:34:27

您无法投影到实体类型。这就是限制。如果您想返回投影(调用select new),您必须返回匿名类型或自定义非实体类型。如果要返回实体类型,则必须始终从 linq-to-entities 返回整列。您可以尝试在对象具体化后使用以下方法修剪列:

 var data = (from ce in entities.ContactEvents
              .Include("Person")
              .Include("Orders")
             where ce.PersonID = personID
             orderby ce.DateTimeContact descending
             select ce)
             .AsEnumerable()
             .Select(e => new ContactEvent 
             {
                  ID = e.ID,
                  DateTimeContact = e.DateTimeContact,
                  Description = e.Description.Substring(0, 500),
                  Orders = e.Orders
             });

You cannot project to entity types. That is the limitation. If you want to return projection (calling select new) you must either return anonymous type or custom non entity type. If you want to return entity type you must always return whole column from linq-to-entities. You can try to trim the column after object is materialized by using:

 var data = (from ce in entities.ContactEvents
              .Include("Person")
              .Include("Orders")
             where ce.PersonID = personID
             orderby ce.DateTimeContact descending
             select ce)
             .AsEnumerable()
             .Select(e => new ContactEvent 
             {
                  ID = e.ID,
                  DateTimeContact = e.DateTimeContact,
                  Description = e.Description.Substring(0, 500),
                  Orders = e.Orders
             });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文