我想检索数据,并设置添加到实体对象(通过部分类)的属性。
我该怎么做?
这是我的查询:
var ret = (from item in contex.Items
select new Itemm
{
Title = item.Title,
Count = 1 // more complex: Count =item.refToAnotherEntity.Count()
});
public partial class Itemm
{
public int Count { get; set; }
}
我得到的错误是:无法在 LINQ to Entities 查询中构造实体或复杂类型
最佳答案
问题是 Linq to Entities 试图将整个表达式转换为 SQL。你的"new"声明是不可能的。
尝试以下操作:
var ret = (from item in contex.Items
select new Itemm
).ToList();
ret = (from item in ret
select new Itemm
{
Title = item.Title,
Count = 1
}).ToList();
基本上,您要确保在尝试重新构建结果之前从 SQL 服务器返回结果(ToList() 执行此操作)。它不是很漂亮,您可能可以稍微清理一下,但上面的代码应该可以工作。
关于c# - 通过 Linq-to-entities 设置扩展属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4786142/