/// <summary> /// Determines whether all elements of a sequence satisfy a condition. /// </summary> /// <param name="source">An <see cref="IBindableCollection{TElement}" /> that contains the elements to apply the predicate to.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> /// <returns> /// true if every element of the source sequence passes the test in the specified /// predicate, or if the sequence is empty; otherwise, false. /// </returns> public static IBindable <bool> All <TSource>(this IBindableCollection <TSource> source, Expression <Func <TSource, bool> > predicate) where TSource : class { source.ShouldNotBeNull("source"); predicate.ShouldNotBeNull("predicate"); return(source.Where(predicate).Count() .Switch() .Case(count => count >= 1, true) .Default(false) .EndSwitch()); }
public PizzaCustomizer(Pizza pizza) { _pizza = pizza; _availableToppings = _pizza.AvailableToppings .AsBindable() .Select(topping => new SelectableTopping(topping)); _selectedToppings = _availableToppings .Where(selectableTopping => selectableTopping.IsSelected) .Select(selectableTopping => selectableTopping.Topping); _healthWarningMessage = _selectedToppings.Count() .Switch() .Case(0, "Surely you would like more toppings?") .Case(toppings => toppings >= 3, "Too many toppings!") .Default( "Perfecto!") .EndSwitch(); _totalPrice = _selectedToppings.Sum(topping => topping.Price).Project(toppingsTotal => toppingsTotal + pizza.BasePrice); }
public PizzaCustomizer(Pizza pizza) { _pizza = pizza; _availableToppings = _pizza.AvailableToppings .AsBindable() .Select(topping => new SelectableTopping(topping)); _selectedToppings = _availableToppings .Where(selectableTopping => selectableTopping.IsSelected) .Select(selectableTopping => selectableTopping.Topping); _healthWarningMessage = _selectedToppings.Count() .Switch() .Case(0, "Surely you would like more toppings?") .Case(toppings => toppings >= 3, "Too many toppings!") .Default( "Perfecto!") .EndSwitch(); _totalPrice = _selectedToppings.Sum(topping => topping.Price).Project(toppingsTotal => toppingsTotal + pizza.BasePrice); }
/// <summary>Returns a number that represents how many elements in the specified sequence satisfy a condition.</summary> /// <returns>A number that represents how many elements in the sequence satisfy the condition in the predicate function.</returns> /// <param name="source">A sequence that contains elements to be tested and counted.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="source" /> or <paramref name="predicate" /> is null.</exception> /// <exception cref="T:System.OverflowException">The number of elements in <paramref name="source" /> is larger than <see cref="F:System.Int32.MaxValue" />.</exception> public static IBindable <int> Count <TSource>(this IBindableCollection <TSource> source, Expression <Func <TSource, bool> > predicate) where TSource : class { predicate.ShouldNotBeNull("predicate"); return(source.Where(predicate).Count()); }
/// <summary> /// Determines whether any element of a sequence satisfies a condition. /// </summary> /// <param name="source">An <see cref="IBindableCollection{TElement}" /> whose elements to apply the predicate to.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> /// <returns>true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.</returns> public static IBindable <bool> Any <TSource>(this IBindableCollection <TSource> source, Expression <Func <TSource, bool> > predicate) where TSource : class { source.ShouldNotBeNull("source"); predicate.ShouldNotBeNull("predicate"); return(source.Where(predicate).Any()); }
/// <summary>Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.</summary> /// <returns>default(<typeparamref name="TSource" />) if <paramref name="source" /> is empty or if no element passes the test specified by <paramref name="predicate" />; otherwise, the first element in <paramref name="source" /> that passes the test specified by <paramref name="predicate" />.</returns> /// <param name="source">An <see cref="IBindableCollection{TElement}" /> to return an element from.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="source" /> or <paramref name="predicate" /> is null.</exception> public static IBindable <TSource> FirstOrDefault <TSource>(this IBindableCollection <TSource> source, Expression <Func <TSource, bool> > predicate) where TSource : class { return(source.Where(predicate).FirstOrDefault()); }
/// <summary>Determines whether a sequence contains a specified element by using a specified <see cref="T:System.Collections.Generic.IEqualityComparer`1" />.</summary> /// <returns>true if the source sequence contains an element that has the specified value; otherwise, false.</returns> /// <param name="source">A sequence in which to locate a value.</param> /// <param name="value">The value to locate in the sequence.</param> /// <param name="comparer">An equality comparer to compare values.</param> /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="source" /> is null.</exception> public static IBindable <bool> Contains <TSource>(this IBindableCollection <TSource> source, TSource value, IEqualityComparer <TSource> comparer) where TSource : class { comparer = comparer ?? new DefaultComparer <TSource>(); value.ShouldNotBeNull("value"); return(source.Where(s => comparer.Equals(s, value)).Any()); }