Пример #1
0
        /// <summary>
        /// Projects each element of a <paramref name="indexedSequence"/> into a form provided by the given <paramref name="selector"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <typeparam name="TResult">Type of object resulting the transform function</typeparam>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="selector">Transform function</param>
        /// <returns>Collection of transformed items</returns>
        public static IEnumerable <TResult> Select <TResult>(this IIndexedEnumerable indexedSequence,
                                                             Func <int, object, TResult> selector)
        {
            int i = -1;

            for (IEnumerator itr = indexedSequence.GetEnumerator(); itr.MoveNext();)
            {
                yield return(selector(++i, itr.Current));
            }
        }
Пример #2
0
        /// <summary>
        /// Applies the given strong-typed <paramref name="action"/> to each element of the given <paramref name="indexedSequence"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <typeparam name="TItem">Type of elements to iterate</typeparam>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="action">Action to apply to each element</param>
        public static void ForEach <TItem>(this IIndexedEnumerable <TItem> indexedSequence, Action <int, TItem> action)
        {
            int i = -1;

            using (IEnumerator <TItem> itr = indexedSequence.GetEnumerator()) {
                while (itr.MoveNext())
                {
                    action(++i, itr.Current);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Projects each strong-typed element a <paramref name="indexedSequence"/> into a form provided by the given <paramref name="selector"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <typeparam name="TItem">Type of elements to iterate</typeparam>
        /// <typeparam name="TResult">Type of object resulting the transform function</typeparam>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="selector">Transform function</param>
        /// <returns>Collection of transformed items</returns>
        public static IEnumerable <TResult> Select <TItem, TResult>(this IIndexedEnumerable <TItem> indexedSequence,
                                                                    Func <int, TItem, TResult> selector)
        {
            int i = -1;

            using (IEnumerator <TItem> itr = indexedSequence.GetEnumerator()) {
                while (itr.MoveNext())
                {
                    yield return(selector(++i, itr.Current));
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Applies the given <paramref name="action"/> to each element of the given <paramref name="indexedSequence"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="action">Action to apply to each element</param>
        public static void ForEach(this IIndexedEnumerable indexedSequence, Action <int, object> action)
        {
            if (indexedSequence == null)
            {
                throw new NullReferenceException();
            }
            IEnumerator itr = indexedSequence.GetEnumerator();

            try {
                int i = -1;
                while (itr.MoveNext())
                {
                    action(++i, itr.Current);
                }
            } finally {
                (itr as IDisposable)?.Dispose();
            }
        }
Пример #5
0
        public static TValue GetEntity <TKey, TValue>(this XElement element, string name, IIndexedEnumerable <TKey, TValue> entitySource, Func <string, TKey> keySelector)
        {
            var attribute = element.Attribute(name);

            if (attribute == null)
            {
                return(default(TValue));
            }

            return(entitySource[keySelector(attribute.Value)]);
        }
Пример #6
0
 public static TValue GetEntity <TKey, TValue>(this XElement element, Expression <Func <TValue> > propertyExpression, IIndexedEnumerable <TKey, TValue> entitySource, Func <string, TKey> keySelector)
 {
     return(GetEntity <TKey, TValue>(element, PropertySupport.ExtractPropertyName(propertyExpression), entitySource, keySelector));
 }
Пример #7
0
        public static bool SetEntity <TKey, TValue>(this XElement element, string name, IIndexedEnumerable <TKey, TValue> entitySource, TValue value, Func <TValue, string> valueSelector)
        {
            var attribute = element.Attribute(name);

            if (attribute == null)
            {
                attribute = element.AddAttribute(new XAttribute(name, string.Empty));
            }

            if (value == null)
            {
                return(!string.IsNullOrEmpty(attribute.Value));
            }

            if (attribute.Value == valueSelector(value))
            {
                return(false);
            }

            attribute.Value = valueSelector(value);

            return(true);
        }