Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

Tags: ienumberable

comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Since writing the last post on this, I refined these methods a bit.

  1. Imports System.Runtime.CompilerServices
  2.  
  3. Namespace Extensions
  4.     ‘'’ <summary>
  5.     ‘'’
  6.     ‘'’ </summary>
  7.     ‘'’ <remarks></remarks>
  8.     Public Module IEnumerableExtensions
  9.  
  10.         ‘'’ <summary>
  11.         ‘'’ Sees if the collection contains any of the parameters. If it finds one then it will return true else it will return false.
  12.         ‘'’ </summary>
  13.         ‘'’ <typeparam name="T">any object</typeparam>
  14.         ‘'’ <param name="Collection">an IEnumerable of T</param>
  15.         ‘'’ <param name="Parameters">an IEnumerable of T</param>
  16.         ‘'’ <returns>True if at least one found, false if not found</returns>
  17.         ‘'’ <remarks>This method equals the OR operator for all elements in Parameter collection. So if Collection contains parameter1 OR parameter2 OR parametern then it will return true.</remarks>
  18.         <Extension()> _
  19.         Public Function ContainsAny(Of T)(ByVal Collection As IEnumerable(Of T), ByVal Parameters As IEnumerable(Of T)) As Boolean
  20.             Return Collection.ContainsAny(Parameters.ToArray)
  21.         End Function
  22.  
  23.         ‘'’ <summary>
  24.         ‘'’ Sees if the collection contains all of the parameters. if it finds all then it will return true else it will return false.
  25.         ‘'’ </summary>
  26.         ‘'’ <typeparam name="T">any object</typeparam>
  27.         ‘'’ <param name="Collection">an IEnumerable of T</param>
  28.         ‘'’ <param name="Parameters">an IEnumerable of T</param>
  29.         ‘'’ <returns>True if all found, false if one not found</returns>
  30.         ‘'’ <remarks>This method equals the AND operator for all elements in Parameter collection. So if Collection contains parameter1 AND parameter2 AND parametern then it will return true.</remarks>
  31.         <Extension()> _
  32.         Public Function ContainsAll(Of T)(ByVal Collection As IEnumerable(Of T), ByVal Parameters As IEnumerable(Of T)) As Boolean
  33.             Return Collection.ContainsAll(Parameters.ToArray)
  34.         End Function
  35.  
  36.         ‘'’ <summary>
  37.         ‘'’ Sees if the collection contains any of the parameters. If it finds one then it will return true else it will return false.
  38.         ‘'’ </summary>
  39.         ‘'’ <typeparam name="T">any object</typeparam>
  40.         ‘'’ <param name="Collection">an array of T</param>
  41.         ‘'’ <param name="Parameters">an array of T</param>
  42.         ‘'’ <returns>True if at least one found, false if not found</returns>
  43.         ‘'’ <remarks>This method equals the OR operator for all elements in Parameter collection. So if Collection contains parameter1 OR parameter2 OR parametern then it will return true.</remarks>
  44.         <Extension()> _
  45.         Public Function ContainsAny(Of T)(ByVal Collection As IEnumerable(Of T), ByVal Parameters As T()) As Boolean
  46.             For Each Parameter As T In Parameters
  47.                 If Collection.Contains(Parameter) Then
  48.                     Return True
  49.                 End If
  50.             Next
  51.             Return False
  52.         End Function
  53.  
  54.         ‘'’ <summary>
  55.         ‘'’ Sees if the collection contains all of the parameters. if it finds all then it will return true else it will return false.
  56.         ‘'’ </summary>
  57.         ‘'’ <typeparam name="T">any object</typeparam>
  58.         ‘'’ <param name="Collection">an array of T</param>
  59.         ‘'’ <param name="Parameters">an array of T</param>
  60.         ‘'’ <returns>True if all found, false if one not found</returns>
  61.         ‘'’ <remarks>This method equals the AND opearator for all elements in Parameter collection. So if Collection contains parameter1 AND parameter2 AND parametern then it will return true.</remarks>
  62.         <Extension()> _
  63.         Public Function ContainsAll(Of T)(ByVal Collection As IEnumerable(Of T), ByVal Parameters As T()) As Boolean
  64.             For Each Parameter As T In Parameters
  65.                 If Not Collection.Contains(Parameter) Then
  66.                     Return False
  67.                 End If
  68.             Next
  69.             Return True
  70.         End Function
  71.     End Module
  72. End Namespace

This one now takes IEnumerable as the type (since ICollection inherits from IEnumerable it just broadens the scope) and it takes an IEnumerable or an array as parameters. Seems to do everything I want it to do right know.

And how do I know that? Because of 24 Unittests I wrote. I won’t post them here, but I have them on the wiki.

About the Author

User bio imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
860 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback