private void GenerateKeyAndAffectedEntitySets(IQueryable <T> source, string customKey = null)
            {
                var objectQuery = DbCacheUtil.GetObjectQuery(source) ?? source as ObjectQuery;

                var key = customKey.NullEmpty();

                if (objectQuery != null && string.IsNullOrEmpty(key))
                {
                    var commandInfo = objectQuery.GetCommandInfo();

                    var sb = new StringBuilder();
                    sb.AppendLine(commandInfo.Sql);

                    foreach (DbParameter parameter in commandInfo.Parameters)
                    {
                        sb.Append(parameter.ParameterName);
                        sb.Append(";");
                        sb.Append(parameter.Value);
                        sb.AppendLine(";");
                    }

                    key = sb.ToString();
                }

                Key = key;
                AffectedEntitySets = objectQuery != null
                                        ? objectQuery.GetAffectedEntitySets()
                                        : new string[0];
            }
 private void EnsureObjectQuery()
 {
     if (!_objectQueryResolved && _objectQuery == null)
     {
         _objectQuery         = DbCacheUtil.GetObjectQuery(_source) ?? _source as ObjectQuery;
         _objectQueryResolved = true;
     }
 }
        /// <summary>
        /// Forces query results to be cached.
        /// Allows caching results for queries using non-deterministic functions.
        /// </summary>
        /// <typeparam name="T">Query element type.</typeparam>
        /// <param name="source">Query whose results will be cached. Must not be null.</param>
        public static IQueryable <T> Cached <T>(this IQueryable <T> source)
            where T : class
        {
            Guard.NotNull(source, nameof(source));

            // TODO: implement Uncached()

            var objectQuery = DbCacheUtil.GetObjectQuery(source) ?? source as ObjectQuery;

            if (objectQuery != null)
            {
                SingletonQueries.Current.AddCachedQuery(
                    objectQuery.Context.MetadataWorkspace,
                    objectQuery.ToTraceString());
            }

            return(source);
        }