Check if an object is a generic collection

3.2k Views Asked by At

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?

3

There are 3 best solutions below

0
On BEST ANSWER

How about:

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

You could try value.GetType().IsGenericType in combination with your check for IEnumerable.

0
On

What about :

value is IEnumerable<Guid>

It's better if you expect Guid instances, isn't it ?