繁体   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