我知道這裏討論的方法:編譯LINQ查詢和DataLoadOptions ...有一個轉折!
Solving common problems with Compiled Queries in Linq to Sql for high demand ASP.NET websites
...但是,這並不對我的情況工作,因爲我得到一個:
「設置加載選項沒有結果後,不允許有從查詢返回。「
我使用的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);
}
}
我第一次嘗試下探靜態DataLoadOptions到SearchManager的階級是這樣的:
public static readonly DataLoadOptions MyOptions =
(new Func<DataLoadOptions>(() =>
{
var option = new DataLoadOptions();
option.LoadWith<Search>(x => x.Rule);
return option;
}))();
...然後將其提供給在GETALL方法類似語境下
public IQueryable<Search> GetAll()
{
Context.LoadOptions = MyOptions;
return Query.GetAll(Context);
}
......這給了我上面提到的錯誤。這是因爲查詢已編譯,因此不能添加額外的DataLoadOptions?如果是這樣,那麼在編譯查詢之前如何應用DataLoadOptions?
檢查公佈答案,確實接受它,如果你得到了你想要 – 2011-10-17 07:44:53