views:

41

answers:

3

Hi guys.

In DataSet.Tables[0].Columns[0] we have a DataType property. Now, I would like to iterate over Columns and perform some action depending on the Type in DataType. How to do this?

foreach(var c in DataSet.Tables[0].Columns)
{
  if (c.DataType == System.String) {} //error 'string' is a 'type', which is not valid in the given context

}
+7  A: 

Use the typeof operator:

if (c.DataType == typeof(string))
{
    // ...
}
LukeH
Or `(c.DataType is string)`.
Marc
@Marc: That's not the same thing. `c.DataType` is a `Type`, not a `string`. Your code will always evaluate to false.
LukeH
I was wondering what I was missing, thanks! I put that comment hoping that it would be rebuked or confirmed.
Marc
+2  A: 

Try this...

if (c.DataType == typeof(string)) {}
robyaw
+2  A: 
if (c.DataType == typeof(string))
{
    // code
}
Vash