@@ -467,5 +467,57 @@ public static IEnumerable<string> GetDuplicates(this IEnumerable<string> list)
467467
468468 return duplicates . ToArray ( ) ;
469469 }
470+
471+ /// <summary>
472+ /// Get list duplicates
473+ /// </summary>
474+ /// <param name="list">Source list</param>
475+ /// <typeparam name="T">Type of list</typeparam>
476+ /// <returns></returns>
477+ /// <remarks></remarks>
478+ public static IEnumerable < T > GetDuplicates < T > ( this IEnumerable < T > list )
479+ {
480+ var duplicates = list
481+ . GroupBy ( x => x )
482+ . Where ( g => g . Count ( ) > 1 )
483+ . Select ( y => y . Key )
484+ . ToArray ( ) ;
485+
486+ return duplicates . ToArray ( ) ;
487+ }
488+
489+ /// <summary>
490+ /// Execute action for every item
491+ /// </summary>
492+ /// <param name="list">Source list</param>
493+ /// <param name="action">Action to be executed</param>
494+ /// <typeparam name="T">Type of list</typeparam>
495+ /// <returns></returns>
496+ /// <remarks></remarks>
497+ public static void ForEach < T > ( this IEnumerable < T > list , Action < T > action )
498+ {
499+ foreach ( T item in list )
500+ {
501+ action ( item ) ;
502+ }
503+ }
504+
505+ /// <summary>
506+ /// Execute action for every item and return new data
507+ /// </summary>
508+ /// <param name="list">Source list</param>
509+ /// <param name="action">Action to be executed</param>
510+ /// <typeparam name="T">Type of list</typeparam>
511+ /// <returns></returns>
512+ /// <remarks></remarks>
513+ public static IEnumerable < T > ForEachAndReturn < T > ( this IEnumerable < T > list , Action < T > action )
514+ {
515+ foreach ( T item in list )
516+ {
517+ action ( item ) ;
518+ }
519+
520+ return list ;
521+ }
470522 }
471523}
0 commit comments