@@ -8,79 +8,6 @@ namespace DwFramework.Core
88{
99 public static class IEnumerableExtension
1010 {
11- /// <summary>
12- /// 遍历
13- /// </summary>
14- /// <param name="enumerable"></param>
15- /// <param name="action"></param>
16- /// <param name="onException"></param>
17- public static void ForEach ( this IEnumerable enumerable , Action < object > action , Action < object , Exception > onException = null )
18- {
19- foreach ( var item in enumerable )
20- {
21- try
22- {
23- action ? . Invoke ( item ) ;
24- }
25- catch ( Exception ex )
26- {
27- if ( onException == null ) throw ;
28- onException ? . Invoke ( item , ex ) ;
29- }
30- }
31- }
32-
33- /// <summary>
34- /// 遍历
35- /// </summary>
36- /// <typeparam name="T"></typeparam>
37- /// <param name="enumerable"></param>
38- /// <param name="action"></param>
39- /// <param name="onException"></param>
40- public static void ForEach < T > ( this IEnumerable < T > enumerable , Action < T > action , Action < T , Exception > onException = null )
41- {
42- foreach ( var item in enumerable )
43- {
44- try
45- {
46- action ? . Invoke ( item ) ;
47- }
48- catch ( Exception ex )
49- {
50- if ( onException == null ) throw ;
51- onException ? . Invoke ( item , ex ) ;
52- }
53- }
54- }
55-
56- /// <summary>
57- /// 遍历(并行)
58- /// </summary>
59- /// <typeparam name="T"></typeparam>
60- /// <param name="enumerable"></param>
61- /// <param name="action"></param>
62- /// <param name="onException"></param>
63- public static void ForEachParallel < T > ( this IEnumerable < T > enumerable , Action < T > action , Action < T , Exception > onException = null )
64- {
65- Parallel . ForEach ( enumerable , ( item , state ) =>
66- {
67- try
68- {
69- if ( state . ShouldExitCurrentIteration ) return ;
70- action ? . Invoke ( item ) ;
71- }
72- catch ( Exception ex )
73- {
74- if ( onException == null )
75- {
76- state . Stop ( ) ;
77- throw ;
78- }
79- onException ? . Invoke ( item , ex ) ;
80- }
81- } ) ;
82- }
83-
8411 /// <summary>
8512 /// 按字段去重
8613 /// </summary>
@@ -103,7 +30,7 @@ public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TS
10330 /// <param name="values"></param>
10431 public static void AddRange < T > ( this ICollection < T > collection , params T [ ] values )
10532 {
106- values . ForEach ( item => collection . Add ( item ) ) ;
33+ foreach ( var item in values ) collection . Add ( item ) ;
10734 }
10835
10936 /// <summary>
@@ -115,7 +42,7 @@ public static void AddRange<T>(this ICollection<T> collection, params T[] values
11542 /// <param name="values"></param>
11643 public static void AddRangeIf < T > ( this ICollection < T > collection , Func < T , bool > predicate , params T [ ] values )
11744 {
118- values . ForEach ( item => { if ( predicate ( item ) ) collection . Add ( item ) ; } ) ;
45+ foreach ( var item in values ) if ( predicate ( item ) ) collection . Add ( item ) ;
11946 }
12047
12148 /// <summary>
@@ -126,7 +53,7 @@ public static void AddRangeIf<T>(this ICollection<T> collection, Func<T, bool> p
12653 /// <param name="values"></param>
12754 public static void AddRangeIfNotContains < T > ( this ICollection < T > collection , params T [ ] values )
12855 {
129- values . ForEach ( item => { if ( ! collection . Contains ( item ) ) collection . Add ( item ) ; } ) ;
56+ foreach ( var item in values ) if ( ! collection . Contains ( item ) ) collection . Add ( item ) ;
13057 }
13158
13259 /// <summary>
@@ -137,7 +64,8 @@ public static void AddRangeIfNotContains<T>(this ICollection<T> collection, para
13764 /// <param name="predicate"></param>
13865 public static void RemoveWhere < T > ( this ICollection < T > collection , Func < T , bool > predicate )
13966 {
140- collection . Where ( predicate ) . ForEach ( item => collection . Remove ( item ) ) ;
67+ var removable = collection . Where ( predicate ) ;
68+ foreach ( var item in removable ) collection . Remove ( item ) ;
14169 }
14270
14371 /// <summary>
@@ -150,11 +78,11 @@ public static void RemoveWhere<T>(this ICollection<T> collection, Func<T, bool>
15078 public static void InsertAfter < T > ( this IList < T > list , Func < T , bool > predicate , T value )
15179 {
15280 var tmp = list . Select ( ( item , index ) => new { item , index } ) . Where ( p => predicate ( p . item ) ) . OrderByDescending ( p => p . index ) ;
153- tmp . ForEach ( item =>
81+ foreach ( var item in tmp )
15482 {
15583 if ( item . index + 1 == list . Count ) list . Add ( value ) ;
15684 else list . Insert ( item . index + 1 , value ) ;
157- } ) ;
85+ } ;
15886 }
15987
16088 /// <summary>
@@ -167,11 +95,11 @@ public static void InsertAfter<T>(this IList<T> list, Func<T, bool> predicate, T
16795 public static void InsertAfter < T > ( this IList < T > list , int index , T value )
16896 {
16997 var tmp = list . Select ( ( v , i ) => new { Value = v , Index = i } ) . Where ( p => p . Index == index ) . OrderByDescending ( p => p . Index ) ;
170- tmp . ForEach ( item =>
98+ foreach ( var item in tmp )
17199 {
172100 if ( item . Index + 1 == list . Count ) list . Add ( value ) ;
173101 else list . Insert ( item . Index + 1 , value ) ;
174- } ) ;
102+ } ;
175103 }
176104
177105 /// <summary>
0 commit comments