views:

148

answers:

2

I can't figure this one out. Why doesn't T4 locate the IEnumerable type? I'm using Visual Studio 2010. And I just hope someone knows why?

<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ assembly name="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  #>
<#@ import namespace="System" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ output extension=".cs" #>
public static class Tables
{
    <#

    var q = @"
        SELECT 
            tbl.name 'table', 
            col.name 'column' 
        FROM 
            sys.tables tbl
        INNER JOIN 
            sys.columns col ON col.object_id = tbl.object_id
    ";

    // var source = Execute(q);

    #>
}
<#+
    static IEnumerable Execute(string cmdText)
    {
        using (var conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=t4build;Integrated Security=True;"))
        {
            conn.Open();

            var cmd = new SqlCommand(cmdText, conn);

            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                }
            }
        }
    }
#>

Error 2 Compiling transformation: The type or namespace name 'IEnumerable' could not be found (are you missing a using directive or an assembly reference?) c:\Projects\T4BuildApp\T4BuildApp\TextTemplate1.tt 26 9

+3  A: 

Probably because IEnumerable is in System.Collections.

Femaref
....and John - you're not importing the `System.Collections` namespace into your template
marc_s
Oh, how could I've not seen this. Thanks! I must have been going blind for an hour or so on this...
John Leidegren
A: 

I would also recommend to referece #assembly name="System.Core" and #import "System.Linq" so you get more power when doing something with IEnumerable

Tim Fischer