-- 作者:逛逛
-- 发布时间:2015/9/6 12:09:00
-- [分享]逛逛之类2 集合常用操作类
集合的常用操作,去重、差集、交集、并集等
封装成Dll,使用此类必须有.net3.5的支持
此主题相关图片如下:jdfw.gif
代码:就是System.Linq的简单封装
Public Class LinqEx
\'\'\' <summary> \'\'\' 获取数组或集合的不重复值 \'\'\' </summary> \'\'\' <typeparam name="TSource">输入序列中的元素的类型</typeparam> \'\'\' <param name="source">要从中移除重复元素的序列</param> \'\'\' <returns>一个 IEnumerable,包含源序列中的非重复元素</returns> \'\'\' <remarks></remarks> Public Shared Function Distinct(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IEnumerable(Of TSource) Return source.Distinct() End Function
\'\'\' <summary> \'\'\' 获取数组或集合的差集 \'\'\' </summary> \'\'\' <typeparam name="TSource">输入序列中的元素的类型</typeparam> \'\'\' <param name="first">一个 IEnumerable,将返回其也不在 second 中的元素</param> \'\'\' <param name="second">一个 IEnumerable,如果它的元素也出现在第一个序列中,则将导致从返回的序列中移除这些元素</param> \'\'\' <returns>包含两个序列元素的差集的序列</returns> \'\'\' <remarks></remarks> Public Shared Function Except(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource) Return first.Except(second) End Function
\'\'\' <summary> \'\'\' 获取数组或集合的交集 \'\'\' </summary> \'\'\' <typeparam name="TSource"></typeparam> \'\'\' <param name="first"></param> \'\'\' <param name="second"></param> \'\'\' <returns></returns> \'\'\' <remarks></remarks> Public Shared Function Intersect(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource) Return first.Intersect(second) End Function
\'\'\' <summary> \'\'\' 获取数组或集合的并集 \'\'\' </summary> \'\'\' <typeparam name="TSource"></typeparam> \'\'\' <param name="first"></param> \'\'\' <param name="second"></param> \'\'\' <returns></returns> \'\'\' <remarks></remarks> Public Shared Function Union(Of TSource)(ByVal first As IEnumerable(Of TSource), ByVal second As IEnumerable(Of TSource)) As IEnumerable(Of TSource) Return first.Union(second) End Function
\'\'\' <summary> \'\'\' 计算 Decimal 值序列之和 \'\'\' </summary> \'\'\' <param name="source">一个要计算和的 Decimal 值序列</param> \'\'\' <returns>序列值之和</returns> \'\'\' <remarks></remarks> Public Shared Function Sum(ByVal source As IEnumerable(Of Decimal)) As Decimal Return source.Sum() End Function
\'\'\' <summary> \'\'\' 计算 Double 值序列之和 \'\'\' </summary> \'\'\' <param name="source">一个要计算和的 Double 值序列</param> \'\'\' <returns>序列值之和</returns> \'\'\' <remarks></remarks> Public Shared Function Sum(ByVal source As IEnumerable(Of Double)) As Double Return source.Sum() End Function
\'\'\' <summary> \'\'\' 计算Integer值序列之和 \'\'\' </summary> \'\'\' <param name="source">一个要计算和的Integer值序列</param> \'\'\' <returns>序列值之和</returns> \'\'\' <remarks></remarks> Public Shared Function Sum(ByVal source As IEnumerable(Of Integer)) As Integer Return source.Sum() End Function
\'\'\' <summary> \'\'\' 计算 Long 值序列之和 \'\'\' </summary> \'\'\' <param name="source">一个要计算和的 Long 值序列</param> \'\'\' <returns>序列值之和</returns> \'\'\' <remarks></remarks> Public Shared Function Sum(ByVal source As IEnumerable(Of Long)) As Long Return source.Sum() End Function
\'\'\' <summary> \'\'\' 计算 Single 值序列之和 \'\'\' </summary> \'\'\' <param name="source">一个要计算和的 Single 值序列</param> \'\'\' <returns>序列值之和</returns> \'\'\' <remarks></remarks> Public Shared Function Sum(ByVal source As IEnumerable(Of Single)) As Single Return source.Sum() End Function
\'\'\' <summary> \'\'\' 返回泛型序列中的最大值 \'\'\' </summary> \'\'\' <typeparam name="TSource"></typeparam> \'\'\' <param name="source"></param> \'\'\' <returns></returns> \'\'\' <remarks></remarks> Public Shared Function Max(Of TSource)(ByVal source As IEnumerable(Of TSource)) As TSource Return source.Max End Function
\'\'\' <summary> \'\'\' 返回泛型序列中的最小值。 \'\'\' </summary> \'\'\' <typeparam name="TSource">source 中的元素的类型。</typeparam> \'\'\' <param name="source">一个值序列,用于确定最小值</param> \'\'\' <returns>序列中的最小值</returns> \'\'\' <remarks></remarks> Public Shared Function Min(Of TSource)(ByVal source As IEnumerable(Of TSource)) As TSource Return source.Min End Function
End Class
示例:别忘了复制Dll
[此贴子已经被作者于2015/10/4 11:02:13编辑过]
|