Search code examples
c#collectionstype-equivalence

Check if an object is a generic collection


We are dynamically building some SQL statements and we are utilizing the IN operator. If our value is a collection of values such that:

List<Guid> guids = new List<Guid>()

I want to be able to provider 'guids' to my clause builder, have it verify the type and if it is enumerable create a clause like:

IN ( {Guid1}, {Guid2}, {Guid3} )

Checking that the value is IEnumerable like this:

if (value is IEnumerable)

falls down when a string is passed in (which happens pretty regularly :) ). What is the best way to validate this type of condition?


Solution

  • How about:

    if(value .GetType().IsGenericType && value is IEnumerable)