簡體   English   中英

在MVC Web應用程序中將Linq編譯為SQL查詢

[英]Compiled Linq to SQL Queries in MVC Webapplication

我已經用mvc 4構建了一個Web應用程序。首先,我在沒有編譯查詢的情況下實現了該應用程序,但是為了提高性能,我想使用編譯查詢。 但是由於DataContext我無法使用查詢。 我有一個查詢類,其中包含許多方法,例如:

    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一起使用。 我也不想在會話中使用DataContext

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

我發現了問題。 我需要一個自己的DataContext子類,其中包含表和其中的連接。 所以,現在我可以使用我的編譯查詢了:)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM