【问题标题】:Compiled Linq to SQL Queries in MVC Webapplication在 MVC Web 应用程序中编译 Linq to SQL 查询
【发布时间】:2014-01-27 10:37:40
【问题描述】:

我已经使用 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();
    }

DataContextController 类中创建和处置。所以我有一个问题,即不可能使用具有不同 DataContexts 的编译查询。 我也不想在会话中使用DataContext

有人想解决我的问题吗?

【问题讨论】:

    标签: c# asp.net-mvc linq-to-sql datacontext linq.compiledquery


    【解决方案1】:

    我发现了我的问题。我需要一个自己的 DataContext 部分类,其中包含我的表和其中的连接。所以,现在我可以使用我编译的查询了:)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      相关资源
      最近更新 更多