Condividi tramite


How to: Create a LINQ Project

The .NET Framework version 3.5 introduces namespaces and references that are required for basic LINQ functionality in Visual C# and Visual Basic. Just create a new project and you can start writing LINQ queries against object collections. Visual Basic additionally provides a reference and imported namespace for LINQ to XML functionality. In Visual C# these must be added manually.

To use LINQ to XML or LINQ to DataSet in either language, you must manually add namespaces and references as described in the following sections.

If you are upgrading a project that you created by using an earlier version of Visual Studio, you may have to supply these or other LINQ-related references manually and also manually set the project to target .NET Framework version 3.5.

Note

If you are building at a command prompt, you must manually reference the LINQ -related DLLs in drive:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5.

Procedures to add LINQ Namespaces and References

To target the .NET Framework version 3.5

  1. In Visual Studio, open a Visual Basic or C# project that was created in Visual Studio 2005 and follow the prompts to convert it to a Visual Studio 2008 or a Visual Studio 2010 project.

  2. For a C# project, click the Project menu, and then click Properties.

    1. In the Application property page, select .NET Framework 3.5 in the Target Framework drop-down list.
  3. For a Visual Basic project, click the Project menu, and then click Properties.

    In the Compile property page, click Advanced Compile Options and then select .NET Framework 3.5 in the Target Framework (all configurations) drop-down list.

To enable basic LINQ functionality

  1. In a Visual Basic or C# project, click the Project menu, and then click Add Reference.

  2. In the Add Reference dialog box, click the .NET tab, scroll to System.Core.dll, and then click it. Click OK.

  3. Add a using directive or Imports statement for System.Linq to your source code file or project.

    For more information, see using Directive (C# Reference) or How to: Add or Remove Imported Namespaces (Visual Basic).

To enable advanced LINQ functionality with expression trees

To use LINQ to XML

  1. If necessary, follow the steps earlier in this topic to add a reference to System.Core.dll and a using directive or Imports statement for System.Linq.

  2. Add a reference to System.Xml.Linq.

  3. Add a using directive or Imports statement for System.Xml.Linq.

    Note

    By default, this functionality is provided for Visual Basic projects.

    For more information, see LINQ to XML [from BPUEDev11].

To use LINQ to SQL

  1. If necessary, follow the steps earlier in this topic to add a reference to System.Core.dll and a using directive or Imports statement for System.Linq.

  2. Add a reference to System.Data.Linq.

  3. Add a using directive or Imports statement for System.Data.Linq or one of the other System.Data.Linq namespaces depending on the requirements of your particular project.

    For more information, see LINQ to SQL [LINQ to SQL].

To use LINQ to Dataset

  1. If necessary, follow the steps earlier in this topic to add a reference to System.Core.dll and a using directive or Imports statement for System.Linq.

  2. Add a reference to System.Data.DataSetExtensions.dll for LINQ to DataSet functionality. Add a reference to System.Data.dll if it does not already exist.

  3. Add a using directive or Imports statement for System.Data, and optionally for System.Data.Common, System.Data.SqlClient, depending on how you connect to the database.

    For more information, see LINQ to DataSet.

See Also

Reference

using Directive (C# Reference)

Other Resources

LINQ (Language-Integrated Query)