【发布时间】:2013-10-30 01:27:15
【问题描述】:
我有来自实体框架的这个实体: Parent、Child、GrandChild 和等效实体 ParentModel 和 ChildModel。
简化:
class ParentModel
{
public IList<ChildModel> Children { get; set; }
}
class ChildModel
{
public string GrandChild { get; set; }
}
在父扩展上我有方法“ToModel”
public static IEnumerable<ProductCategoryModel> ToModel(
this IQueryable<ProductCategory> query)
{
IList<ParentModel> model =
query.Select(p => new ParentModel {
Childs = p.Childs.Select(ch => new ChildModel {
Grandchild = ch.Grandchild.Code
}).ToList()
}).ToList();
return model;
}
问题是它不起作用。 我知道为什么 - 嵌套的 ToList() 方法不能在 DB 端运行。
是否有任何简单的解决方案如何编写正确的等效代码并且可以正常工作,会很简单?我在 foreach 中看到了一些解决方案,但在我看来,它不会很好。
【问题讨论】:
-
“它不起作用”并没有告诉我们发生了什么。你有例外吗?它会给出空的结果吗?还有什么?
标签: c# entity-framework iqueryable ilist