using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace JiShe.CollectBus.Common.Extensions
{
///
/// Extension methods for .
///
public static class EnumerableExtensions
{
///
/// Concatenates the members of a constructed collection of type System.String, using the specified separator between each member.
/// This is a shortcut for string.Join(...)
///
/// A collection that contains the strings to concatenate.
/// The string to use as a separator. separator is included in the returned string only if values has more than one element.
/// A string that consists of the members of values delimited by the separator string. If values has no members, the method returns System.String.Empty.
[Description("Enumerable转换String")]
public static string JoinAsString(this IEnumerable source, string separator)
{
return string.Join(separator, source);
}
///
/// Concatenates the members of a collection, using the specified separator between each member.
/// This is a shortcut for string.Join(...)
///
/// A collection that contains the objects to concatenate.
/// The string to use as a separator. separator is included in the returned string only if values has more than one element.
/// The type of the members of values.
/// A string that consists of the members of values delimited by the separator string. If values has no members, the method returns System.String.Empty.
public static string JoinAsString(this IEnumerable source, string separator)
{
return string.Join(separator, source);
}
///
/// Filters a by given predicate if given condition is true.
///
/// Enumerable to apply filtering
/// A boolean value
/// Predicate to filter the enumerable
/// Filtered or not filtered enumerable based on
[Description("Enumerable筛选")]
public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate)
{
return condition
? source.Where(predicate)
: source;
}
///
/// Filters a by given predicate if given condition is true.
///
/// Enumerable to apply filtering
/// A boolean value
/// Predicate to filter the enumerable
/// Filtered or not filtered enumerable based on
public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate)
{
return condition
? source.Where(predicate)
: source;
}
///
/// 分批
///
///
///
///
///
public static IEnumerable> Batch(
this IEnumerable source,
int batchSize)
{
var buffer = new List(batchSize);
foreach (var item in source)
{
buffer.Add(item);
if (buffer.Count == batchSize)
{
yield return buffer;
buffer = new List(batchSize);
}
}
if (buffer.Count > 0)
yield return buffer;
}
//public static IEnumerable> Batch(this IEnumerable source, int batchSize)
//{
// if (batchSize <= 0)
// throw new ArgumentOutOfRangeException(nameof(batchSize));
// using var enumerator = source.GetEnumerator();
// while (enumerator.MoveNext())
// {
// yield return GetBatch(enumerator, batchSize);
// }
//}
//private static IEnumerable GetBatch(IEnumerator enumerator, int batchSize)
//{
// do
// {
// yield return enumerator.Current;
// batchSize--;
// } while (batchSize > 0 && enumerator.MoveNext());
//}
}
}