none
Can I use LINQ in TextTemplate?

    Question

  • Hi,

    I am using VS 2008 to design DSL,

    but found that LINQ cannot be used in TextTemplate.

     

    Is it possible to use Linq in tt file?

     

    Thank you very much!

    Monday, December 10, 2007 3:06 AM

Answers

  • Yes, you can - this feature was added in VS2008.

     

    In the template directive proceessor you need to:

    1) specify the language version as either "C#v3.5" or "VBv3.5";

    2) add an assembly directive to "System.Core.dll" so that the compiler picks up the core assembly; and

    3) add an "import" directive to the Linq namespace.

     

    e.g.

     

    <#@ template language="C#v3.5" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"  #>
    <#@ assembly name="System.Core.dll" #>
    <#@ import namespace="System.Linq" #>

     

    Duncan

    Monday, December 10, 2007 12:42 PM

All replies

  • Yes, you can - this feature was added in VS2008.

     

    In the template directive proceessor you need to:

    1) specify the language version as either "C#v3.5" or "VBv3.5";

    2) add an assembly directive to "System.Core.dll" so that the compiler picks up the core assembly; and

    3) add an "import" directive to the Linq namespace.

     

    e.g.

     

    <#@ template language="C#v3.5" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"  #>
    <#@ assembly name="System.Core.dll" #>
    <#@ import namespace="System.Linq" #>

     

    Duncan

    Monday, December 10, 2007 12:42 PM
  • Incredible!

    Thank you, Thank you! Duncan!

     

    Linq will be very helpful to iterate and find the object existed in the domain model.

     

    Thank you so much!

    Monday, December 10, 2007 11:09 PM
  • Yes!
    +1 Thanks from me.
    Tuesday, July 15, 2008 12:51 PM
  • Hi, I cannot get this to work.  Any ideas?

    <#@ template language="C#v3.5" debug="True" inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #>
    <#@ assembly name="System.Core.dll" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#@ assembly name="C:\Solutions\Debug\MyNameSpace.MyAssembly.Business\bin\Debug\Business.dll" #>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MyNameSpace.MyAssembly.Business;

    namespace MyNameSpace.MyAssembly.Business
    {
     public enum MatterFileNoteTypes2 : int
     {
      <#
       IEnumerable<MyNameSpace.MyAssembly.Business.MatterFileNoteType> items = 
        MyNameSpace.MyAssembly.Business.MatterFileNoteType.GetMatterFileNoteTypes();
       foreach(var item in items)
       {
        WriteLine(item.Name);
       }
      #>
     }
    }

    I get the error: Running transformation: System.NullReferenceException.

    Thanks,
    Derick

    • Edited by DerickZA Thursday, July 09, 2009 1:59 PM
    Thursday, July 09, 2009 1:58 PM
  • Try adding a System.Diagnostics.Debugger.Break(); statement to your .tt, then run the transformation again.

    You should then be able to step into the debugger to debug your template code (for this to work, you need to have <#@ template debug="True" ... #>, which you already do).

    Regards,
    Duncan
    Thursday, July 09, 2009 6:13 PM