kri*_*isg 6 c# linq loadoptions compiled-query
我知道这里讨论的方法:
解决Linq to Sql中针对高需求ASP.NET网站的编译查询的常见问题
...但这对我的情况不起作用,因为我得到了:
"从查询返回结果后,不允许设置加载选项."
我使用Codesmith PLINQO脚本生成实体和管理器代码,管理器代码如下所示:
public partial class SearchManager
{
#region Query
// A private class for lazy loading static compiled queries.
private static partial class Query
{
internal static readonly Func<MyDataContext,IOrderedQueryable<Search>>
GetAll = CompiledQuery.Compile(
(MyDataContext db) =>
from s in db.Search
orderby s.Name
select s);
}
#endregion
public IQueryable<Search> GetAll()
{
return Query.GetAll(Context);
}
}
Run Code Online (Sandbox Code Playgroud)
我首先尝试将静态DataLoadOptions拖放到Searchmanager类中,如下所示:
public static readonly DataLoadOptions MyOptions =
(new Func<DataLoadOptions>(() =>
{
var option = new DataLoadOptions();
option.LoadWith<Search>(x => x.Rule);
return option;
}))();
Run Code Online (Sandbox Code Playgroud)
...然后在GetAll方法中将其提供给Context,如:
public IQueryable<Search> GetAll()
{
Context.LoadOptions = MyOptions;
return Query.GetAll(Context);
}
Run Code Online (Sandbox Code Playgroud)
......这给了我上面提到的错误.这是因为查询已经编译,因此不能添加"额外"DataLoadOptions吗?如果是这样,如何在编译查询之前应用DataLoadOptions?
错误消息本身会告诉您到底出了什么问题。Linq 查询返回结果后,您无法应用 DataLoadOptions。或者,也许更好的说法如下。如果要应用 DataLoadOptions,请在执行查询之前执行此操作。之后您不能这样做。