【发布时间】:2015-01-21 18:40:20
【问题描述】:
Here 我学会了如何在不加载相关实体的情况下对其进行计数。问题是我在编译时没有实体类型。我的情况:
var postCount = context.Entry(someObject) // someObject received from somewhere
.Collection(somePropertyString)
.Query() // and here I got a non-generic IQueryable
.Count(); // which has no Count method
如果我尝试.Query().Cast<object>().Count(),我会在这一行遇到运行时异常:
System.NotSupportedException 发生 HResult=-2146233067
消息=无法将类型“...”转换为类型 '系统.对象'。 LINQ to Entities 仅支持转换 EDM 原语 或枚举类型。来源=实体框架
那么,如果我在编译时没有实体类型,如何计算相关实体而不加载它们?
【问题讨论】:
-
对我来说,您必须使用反射来获得
Expression<Func<TEntity, ICollection<TElement>>>才能使用Collection的通用版本,这将允许您使用Count()。
标签: c# linq entity-framework generics entity-framework-6