/// <summary> /// Specifies the related objects to include in the query results. /// </summary> /// <typeparam name="T">The type of the data in the data source.</typeparam> /// <typeparam name="TInclude">The type of the include.</typeparam> /// <param name="query">The query to be materialized.</param> /// <param name="includeExpression">The expression of related objects to return in the query results.</param> /// <returns>The result of the query.</returns> //http://damieng.com/blog/2010/05/21/include-for-linq-to-sql-and-maybe-other-providers public static IEnumerable <T> Include <T, TInclude>(this IQueryable <T> query, Expression <Func <T, TInclude> > includeExpression) { var db = LinqToSqlDataContextProvider.GetDataContext(query); if (db == null) { throw new ArgumentException("The query must originate from a DataContext.", "query"); } if (!db.ObjectTrackingEnabled || !db.DeferredLoadingEnabled) { throw new ArgumentException("The query's originating DataContext must have ObjectTrackingEnabled and DeferredLoadingEnabled set to true.", "query"); } var elementParameter = includeExpression.Parameters.Single(); var tupleType = typeof(Tuple <T, TInclude>); var body = Expression.New(tupleType.GetConstructor(new[] { typeof(T), typeof(TInclude) }), new[] { elementParameter, includeExpression.Body }, tupleType.GetProperty("Item1"), tupleType.GetProperty("Item2")); var selector = Expression.Lambda <Func <T, Tuple <T, TInclude> > >(body, elementParameter); return(query.Select(selector).AsEnumerable().Select(t => t.Item1)); }
/// <summary> /// Returns the result of the query; if possible from the cache, otherwise /// the query is materialized and the result cached before being returned. /// </summary> /// <typeparam name="T">The type of the data in the data source.</typeparam> /// <param name="query">The query to be materialized.</param> /// <param name="profileName">Name of the cache profile to use.</param> /// <param name="sqlCacheDependencyTables">The tables for which to add SQL Cache Dependencies</param> /// <returns>The result of the query.</returns> public static IEnumerable <T> FromCache <T>(this IQueryable <T> query, string profileName, params ITable[] sqlCacheDependencyTables) { var db = LinqToSqlDataContextProvider.GetDataContext(query); var cacheSettings = CacheManager .GetProfile(profileName) .AddCacheDependency(db.Connection.Database, sqlCacheDependencyTables); return(query.FromCache(cacheSettings)); }
/// <summary> /// Returns the result of the query; if possible from the cache, otherwise /// the query is materialized and the result cached before being returned. /// Queries, caches, and returns only the first entity. /// </summary> /// <typeparam name="T">The type of the data in the data source.</typeparam> /// <param name="query">The query to be materialized.</param> /// <param name="duration">The amount of time, in seconds, that a cache entry is to remain in the output cache.</param> /// <param name="sqlCacheDependencyTableNames">The table names for which to add SQL Cache Dependencies</param> /// <returns>The first or default result of the query.</returns> public static T FromCacheFirstOrDefault <T>(this IQueryable <T> query, int duration, params string[] sqlCacheDependencyTableNames) { var db = LinqToSqlDataContextProvider.GetDataContext(query); var cacheSettings = new CacheSettings(duration) .AddCacheDependency(db.Connection.Database, sqlCacheDependencyTableNames); return(query .Take(1) .FromCache(cacheSettings) .FirstOrDefault()); }
static DataContextBase() { var provider = new LinqToSqlDataContextProvider(); DataContextProvider.Register(provider); }