实体框架限制返回的 nvarchar 列的长度
我想限制 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法投影到实体类型。这就是限制。如果您想返回投影(调用
select new
),您必须返回匿名类型或自定义非实体类型。如果要返回实体类型,则必须始终从 linq-to-entities 返回整列。您可以尝试在对象具体化后使用以下方法修剪列: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: