using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///
///
///
public partial class Utils
{
}
///
/// 扩展下Linq,增加一个取随机值方法. wanggangzero@vip.qq.coms
///
public static class LinqExt
{
static Random r = new Random(Guid.NewGuid().GetHashCode()); // 随机种子
///
/// 从集合中随机取一批数据
///
///
///
///
///
public static IEnumerable Random(this IEnumerable source, int? count = null)
{
return count.HasValue ? source.OrderBy(_ => r.Next()).Take(count.Value) : source.OrderBy(_ => r.Next());
}
///
/// 仿PHP的implode函数
///
///
///
///
public static string Implode(this IEnumerable source, string glue = "")
{
var sb = new StringBuilder();
var n = source.Count();
int i = 0;
source.ToList().ForEach(s => sb.Append((++i < n) ? s + glue : s));
return sb.ToString();
}
public static string[] Explode(this string strSource, string seperator = ""){
return strSource.Split(new string[] { seperator }, StringSplitOptions.RemoveEmptyEntries);
}
}