2014-01-27 39 views
2

我已經用mvc 4構建了一個web應用程序。首先我實現了沒有編譯查詢的應用程序,但爲了提高我想要使用編譯查詢的性能。 但我不能使用查詢,因爲DataContext。 我對查詢一類具有大量的類似方法:將Linq編譯爲MVC中的SQL查詢Web應用程序

private static Func<DataContext, int, IEnumerable<Information>> _OwnInformations; 
    public static List<Information> GetOwnInformations(DataContext dataContext, int userId) 
    { 
     if (_OwnInformations == null) 
     { 
      _OwnInformations = CompiledQuery.Compile<DataContext, int, IEnumerable<Information>>((dc, id) => (
       from tm in dc.GetTable<Information>() 
       where tm.User.Id == id || tm.Author.Id == id 
       select tm 
       )); 
     } 
      return _OwnInformations.Invoke(dataContext, userId).Distinct().ToList(); 
    } 

DataContext的創建和設置在Controller類。所以我有這樣的問題,即不可能使用不同的編碼查詢DataContext s。 我也不想在會話中使用DataContext

有沒有人想法解決我的問題?

回答

1

我發現我的問題。我需要一個自己的DataContext部分類與我的表和連接內。所以,現在我可以使用我的編譯查詢:)。