Exemplo n.º 1
0
        public static IEnumerable <T> StartsWith <T>(this IEnumerable <T> source, Func <T, string> selector, string from, string to = null)
        {
            if (!source.HasItems())
            {
                return(new List <T>());
            }

            var lowerCaseFrom = from.ToLowerInvariant()[0];
            var upperCaseFrom = from.ToUpperInvariant()[0];

            if (!String.IsNullOrWhiteSpace(to))
            {
                var lowerCaseTo = to.ToLowerInvariant()[0];
                var upperCaseTo = to.ToUpperInvariant()[0];

                return(source.Where(m => (selector(m)[0] >= lowerCaseFrom && selector(m)[0] <= lowerCaseTo) ||
                                    (selector(m)[0] >= upperCaseFrom && selector(m)[0] <= upperCaseTo)));
            }

            return(source.Where(m => selector(m)[0] == lowerCaseFrom || selector(m)[0] == upperCaseFrom));
        }