Yes, of course, you can execute any arbitrary valid .NET code in the T4 template - this template is ultimately converted to a .NET class, which is then compiled and executed.
What is your problem? What did you try to do and how far have you come? Where are the checkpoints?
UPDATE:
I tried the following:
- I put the Linq-to-SQL model in a class library called
AdventureWorksData - Then I created a new project (console application)
- ,
AdventureWorksData assembly
T4 :
<#@ Template Language="C#v3.5" Debug="true" #>
<#@ Output Extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Data.dll" #>
<#@ Assembly Name="System.Data.Linq.dll" #>
<#@ Assembly Name="AdventureWorksData.dll" #>
<#@ Import Namespace="System" #>
<#@ Import Namespace="System.Data" #>
<#@ Import Namespace="System.Data.Linq" #>
<#@ Import Namespace="System.Linq" #>
<#@ Import Namespace="AdventureWorksData" #>
using System;
using AdventureWorksData;
namespace AdventureWorks.Products
{
public class ProductList
{
<#
AdventureWorksDataContext ctx = new AdventureWorksDataContext();
var result = from p in ctx.Products
where p.ProductCategoryID == 5
select p;
foreach (Product product in result)
{
#>
public string <#= SanitizeName(product.Name) #> = "<#= product.ProductNumber #> / ID= <#= product.ProductID #>";
<#
}
#>
}
}
<#+
internal string SanitizeName(string input)
{
return input.Replace(" ", "_").Replace("-", "_").Replace("/", "_").Replace(".", "_").Replace(",", "_");
}
#>
Products AdventureWorksLT, ProductCategoryID == 5, Linq.
, , () , , .
, Linq-to-SQL LINQ T4.