Example #1
0
        /// <summary>
        ///     Filter the query using context filters associated with specified keys.
        /// </summary>
        /// <typeparam name="T">The type of elements of the query.</typeparam>
        /// <param name="query">The query to filter using context filters associated with specified keys.</param>
        /// <param name="keys">
        ///     A variable-length parameters list containing keys associated to context filters to use to filter the
        ///     query.
        /// </param>
        /// <returns>The query filtered using context filters associated with specified keys.</returns>
        public static IQueryable <T> Filter <T>(this IDbSet <T> query, params object[] keys) where T : class
        {
            var filterContext = QueryFilterManager.AddOrGetFilterContext(query.GetDbContext());
            var filterHook    = QueryFilterManager.EnableFilterById;

            var sb = new StringBuilder();

            if (keys != null)
            {
                foreach (var key in keys)
                {
                    var filter = filterContext.GetFilter(key);
                    if (filter == null)
                    {
                        continue;
                    }
                    sb.Append(filter.UniqueKey);
                    sb.Append(";");
                }
            }

            return(QueryFilterManager.HookFilter(query.AsNoFilter(), filterHook + sb));
        }