Ejemplo n.º 1
0
        // -- Best Practice sample extension to create extensions for IEnumerable<T> in NetOffice
        //
        // In order to prevent ambiguous conflicts
        // you need to target NetOffice.CollectionsGeneric.IEnumerableProvider<T>
        // All collections in NetOffice implement these interface
        public static IEnumerable <Excel.Range> AllCellsWithValues(this IEnumerableProvider <Excel.Range> source)
        {
            List <Excel.Range> result     = new List <NetOffice.ExcelApi.Range>();
            ICOMObject         enumerator = source.GetComObjectEnumerator(null);

            try
            {
                foreach (Excel.Range item in source.FetchVariantComObjectEnumerator(source as ICOMObject, enumerator))
                {
                    if (item.Value != null)
                    {
                        result.Add(item);
                    }
                    else
                    {
                        item.Dispose();
                    }
                }
                return(result);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
 public static EnumerableProvider <TSource, TOwner> Take <TSource, TOwner>(
     this IEnumerableProvider <TSource, TOwner> source,
     int count)
 =>
 source.CheckNotNull(nameof(source)).Query(
     $"Take({count})",
     x => x.Take(count));
Ejemplo n.º 3
0
 public static EnumerableProvider <TSource, TOwner> Distinct <TSource, TOwner>(
     this IEnumerableProvider <TSource, TOwner> source,
     IEqualityComparer <TSource> comparer)
 =>
 source.CheckNotNull(nameof(source)).Query(
     $"Distinct({comparer})",
     x => x.Distinct(comparer));
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the number of elements in a sequence
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="predicate">a function to test each element for a condition</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>the number of elements in the input sequence.</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static int Count <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            int        result     = 0;
            ICOMObject enumerator = null;

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                foreach (TSource current in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    if (predicate(current))
                    {
                        result++;
                    }
                    TryDispose(current);
                }
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns the first element of a sequence
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>the first element in the specified sequence</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="InvalidOperationException">sequence is empty</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static TSource First <TSource>(this IEnumerableProvider <TSource> source, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                foreach (TSource item in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    if (null != enumerator && enumerator != source)
                    {
                        enumerator.Dispose();
                    }
                    return(item);
                }
                throw new InvalidOperationException("Sequence is empty.");
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 6
0
        public static TSource Single <TSource, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source)
        {
            TSource value = source.CheckNotNull(nameof(source)).Value.Single();

            (value as IHasProviderName)?.SetProviderName("Single()");

            return(value);
        }
Ejemplo n.º 7
0
        public static TSource FirstOrDefault <TSource, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source)
        {
            TSource value = source.CheckNotNull(nameof(source)).Value.FirstOrDefault();

            (value as IHasProviderName)?.SetProviderName("FirstOrDefault()");

            return(value);
        }
Ejemplo n.º 8
0
        public static EnumerableProvider <TSource, TOwner> Where <TSource, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source,
            Expression <Func <TSource, bool> > predicate)
        {
            var predicateFunction = predicate.CheckNotNull(nameof(predicate)).Compile();

            return(source.CheckNotNull(nameof(source)).Query(
                       $"Where({ConvertToString(predicate)})",
                       x => x.Where(predicateFunction)));
        }
Ejemplo n.º 9
0
        public static TSource ElementAtOrDefault <TSource, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source,
            int index)
        {
            TSource value = source.CheckNotNull(nameof(source)).Value.ElementAtOrDefault(index);

            (value as IHasProviderName)?.SetProviderName($"ElementAtOrDefault({index})");

            return(value);
        }
Ejemplo n.º 10
0
        public static EnumerableProvider <TResult, TOwner> Select <TSource, TResult, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source,
            Expression <Func <TSource, int, TResult> > selector)
        {
            var selectorFunction = selector.CheckNotNull(nameof(selector)).Compile();

            return(source.CheckNotNull(nameof(source)).Query(
                       $"Select({ConvertToString(selector)})",
                       x => x.Select(selectorFunction)));
        }
Ejemplo n.º 11
0
        public static TSource FirstOrDefault <TSource, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source,
            Expression <Func <TSource, bool> > predicate)
        {
            var predicateFunction = predicate.CheckNotNull(nameof(predicate)).Compile();

            TSource value = source.CheckNotNull(nameof(source)).Value.FirstOrDefault(predicateFunction);

            (value as IHasProviderName)?.SetProviderName($"FirstOrDefault({ConvertToString(predicate)})");

            return(value);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Filters a sequence of values based on a predicate
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="predicate">a function to test each element for a condition</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>true if the source sequence contains an element that has the specified value; otherwise, false</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static IEnumerable <TSource> Where <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, int, bool> predicate, bool append = true)
        {
            if (null == source)
            {
                throw new ArgumentNullException("source");
            }
            if (null == predicate)
            {
                throw new ArgumentNullException("predicate");
            }
            ICOMObject     enumerator = null;
            List <TSource> list       = new List <TSource>();

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                int num = -1;
                foreach (TSource item in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    int num2 = num;
                    num = checked (num2 + 1);
                    if (predicate(item, num))
                    {
                        list.Add(item);
                    }
                    else
                    {
                        TryDispose(item);
                    }
                }
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                return(list);
            }
            catch
            {
                for (int i = list.Count - 1; i >= 0; i++)
                {
                    TryDispose(list[i]);
                }
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Returns the last element of a sequence
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <param name="predicate">a function to test each element for a condition</param>
        /// <returns>the value at the last position in the source sequence</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static TSource LastOrDefault <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                IEnumerator enumeratorSource = source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator).GetEnumerator();
                if (enumeratorSource.MoveNext())
                {
                    TSource lastCurrent = default(TSource);
                    TSource current     = default(TSource);
                    do
                    {
                        lastCurrent = current;
                        TSource item = (TSource)enumeratorSource.Current;
                        if (predicate(item))
                        {
                            current = item;
                        }
                        if (null != lastCurrent)
                        {
                            TryDispose(lastCurrent);
                        }
                    }while (enumeratorSource.MoveNext());
                    if (null != current)
                    {
                        return(current);
                    }
                }
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                return(default(TSource));
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Returns the first element of a sequence
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <param name="predicate">a function to test each element for a condition</param>
        /// <returns>the first element in the specified sequence</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="InvalidOperationException">sequence is empty</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static TSource First <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                IEnumerableProvider <TSource> sequence = source as IEnumerableProvider <TSource>;
                if (null == sequence)
                {
                    throw new ArgumentException("Unable to cast IEnumerableProvider<TSource>");
                }
                enumerator = sequence.GetComObjectEnumerator(null);
                foreach (TSource item in sequence.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    if (predicate(item))
                    {
                        if (null != enumerator && enumerator != source)
                        {
                            enumerator.Dispose();
                        }
                        return(item);
                    }
                    else
                    {
                        TryDispose(item);
                    }
                }
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw new InvalidOperationException("No element satisfies the condition in predicate or the source sequence is empty.");
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 15
0
        public static EnumerableProvider <TResult, TOwner> Query <TSource, TResult, TOwner>(
            this IEnumerableProvider <TSource, TOwner> source,
            string valueName,
            Func <IEnumerable <TSource>, IEnumerable <TResult> > valueGetFunction)
        {
            source.CheckNotNull(nameof(source));
            valueName.CheckNotNull(nameof(valueName));
            valueGetFunction.CheckNotNull(nameof(valueGetFunction));

            IObjectSource <IEnumerable <TResult> > valueSource = source.IsValueDynamic
                ? new DynamicObjectSource <IEnumerable <TResult>, IEnumerable <TSource> >(
                source,
                valueGetFunction)
                : (IObjectSource <IEnumerable <TResult> >) new LazyObjectSource <IEnumerable <TResult>, IEnumerable <TSource> >(
                source,
                valueGetFunction);

            return(new EnumerableProvider <TResult, TOwner>(source.Owner, valueSource, valueName));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Returns the first element of a sequence, or a default value if the sequence contains no elements
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="predicate">a function to test each element for a condition</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>default(TSource) if <paramref name="source" /> is empty; otherwise, the first element in <paramref name="source" /></returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static TSource FirstOrDefault <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                foreach (TSource item in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    if (predicate(item))
                    {
                        if (null != enumerator && enumerator != source)
                        {
                            enumerator.Dispose();
                        }
                        return(item);
                    }
                    else
                    {
                        TryDispose(item);
                    }
                }
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                return(default(TSource));
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Determines whether a sequence contains any elements
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>true if the source sequence contains any elements; otherwise, false</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static bool Any <TSource>(this IEnumerableProvider <TSource> source, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                IEnumerableProvider <TSource> sequence = source as IEnumerableProvider <TSource>;
                if (null == sequence)
                {
                    throw new ArgumentException("Unable to cast IEnumerableProvider<TSource>");
                }
                enumerator = sequence.GetComObjectEnumerator(null);
                foreach (TSource current in sequence.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    TryDispose(current);
                    return(true);
                }

                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                return(false);
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Determines whether a sequence contains any elements
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="predicate">a function to test each element for a condition</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>true if the source sequence contains any elements; otherwise, false</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static bool Any <TSource>(this IEnumerableProvider <TSource> source, Func <TSource, bool> predicate, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                foreach (TSource current in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    bool match = predicate(current);
                    TryDispose(current);
                    if (match)
                    {
                        return(true);
                    }
                }

                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                return(false);
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Determines whether a sequence contains a specified element by using the NetOffice Core equality comparer
        /// </summary>
        /// <typeparam name="TSource">the type of the elements of <paramref name="source"/></typeparam>
        /// <param name="source">the <see cref="T:System.Collections.Generic.IEnumerable`1" /> to return the first element of</param>
        /// <param name="value">the value to locate in the sequence</param>
        /// <param name="append">append items in sequence to parent instance in com proxy management</param>
        /// <returns>true if the source sequence contains an element that has the specified value; otherwise, false</returns>
        /// <exception cref="ArgumentNullException">source is null(Nothing in Visual Basic)</exception>
        /// <exception cref="NetOfficeCOMException">error occured while calling remote server</exception>
        public static bool Contains <TSource>(this IEnumerableProvider <TSource> source, TSource value, bool append = true)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            ICOMObject enumerator = null;

            try
            {
                enumerator = source.GetComObjectEnumerator(null);
                foreach (TSource current in source.FetchVariantComObjectEnumerator(true == append ? source as ICOMObject : null, enumerator))
                {
                    bool match = NetOffice.CoreServices.RemoteComparsion.EqualsOnServer(current, value);
                    TryDispose(current);
                    if (match)
                    {
                        return(true);
                    }
                }

                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                return(false);
            }
            catch
            {
                if (null != enumerator && enumerator != source)
                {
                    enumerator.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 20
0
 public static EnumerableProvider <TSource, TOwner> Distinct <TSource, TOwner>(
     this IEnumerableProvider <TSource, TOwner> source)
 =>
 source.CheckNotNull(nameof(source)).Query(
     "Distinct()",
     x => x.Distinct());