public static ISubscribable <T> Partition <T, TKey>(this ISubscribable <T> source, Expression <Func <T, TKey> > filter, IEqualityComparer <TKey> keyComparer, TKey value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (filter == null) { throw new ArgumentNullException(nameof(filter)); } if (keyComparer == null) { throw new ArgumentNullException(nameof(keyComparer)); } if (source is QuotedSubscribable <T> quotedPartitionableSubscribable) { source = quotedPartitionableSubscribable.Value; } if (source is IPartitionableSubscribable <T> partitionableSubscribable) { return(partitionableSubscribable.AddPartition(filter).Bind(value, keyComparer)); } if (source is IPartitionedMultiSubject <T> partitionedMultiSubject) { return(partitionedMultiSubject.CreatePartitionableSubscribable().AddPartition(filter).Bind(value, keyComparer)); } // This is the case where the input is not a partitionable artifact. In practice we should only delegate if the input is partitionable, // so this case should not be hit. var eq = EqualityComparerReflectionHelper <TKey> .EqualsMethod; var p = filter.Parameters.Single(); var f = Expression.Lambda <Func <T, bool> >(Expression.Call(Expression.Constant(keyComparer), eq, filter.Body, Expression.Constant(value)), p); return(source.Where(f.Compile())); }
private static ISubscribable <TSource> AddFilter <TKey>(ISubscribable <TSource> subscribable, KeyBinding <TSource, TKey> binding) { return(subscribable.Where(x => binding.KeyComparer.Equals(binding.KeySelector.Invoke(x), binding.Key))); }