如何解决重用查询和include方法
对于实体查询,我们有一个复杂的LINQ;出于冗余的原因,我们想重用该查询。 一个例子来说明我们的问题:var query = from x in context.Building.Include(\"Kitchen\")
select x;
在另一个用例中,我们希望尽快加载“卧室”
var query = from x in context.Building.Include(\"Bedroom\")
select x;
以后是否可以添加或更改“包含”内容?
我正在寻找这样的东西:
var query = from x in context.Building select x;
query.AddInclude(\"Kitchen\");
query.AddIncldue(\"Other stuff\");
您如何进行查询重用?
解决方法
下载EF 4.1并使用对
IQueryable
的强类型扩展名,或尝试编写write4ѭ,例如:
public static IQueryable<T> AddInclude(this IQueryable<T> query,string include)
{
if (String.IsNullOrEmpty(include))
{
throw new ArgumetNullException(\"include\");
}
var objectQuery = query as ObjectQuery<T>;
if (objectQuery == null)
{
throw new InvalidOperationException();
}
return objectQuery.Include(include);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。