This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

LINQ Types


In C#, LINQ (Language Integrated Query) has several types or providers, each designed to work with different types of data sources. Here are the main types/providers of LINQ.

LINQ Introduction

Types or Providers of LINQ?

There are several types of LINQ:

  • LINQ to Objects
  • LINQ to XML
  • LINQ to SQL
  • LINQ to Entities (Entity Framework)
  • LINQ to DataSet

Here are the main types of LINQ in C# explained:

  1. LINQ to Objects

    • Description - Used to query in-memory collections like arrays, lists, and other IEnumerable<T> collections.

      Example
      int[] numbers = { 1, 2, 3, 4, 5 };
      var evenNumbers = from n in numbers
                        where n % 2 == 0
                        select n;
      
      foreach (var num in evenNumbers)
      {
          Console.WriteLine(num); // Output: 2, 4
      }
  2. LINQ to SQL

    • Description - Used to query SQL Server databases using .NET objects. It translates LINQ queries to SQL queries.

      Example
      using (DataContext context = new DataContext(connectionString))
      {
          var employees = from e in context.GetTable<Employee>()
                          where e.Department == "Sales"
                          select e;
      
          foreach (var employee in employees)
          {
              Console.WriteLine(employee.Name);
          }
      }
  3. LINQ to XML

    • Description - Used to query and manipulate XML documents.

      Example
      XElement xml = XElement.Load("employees.xml");
      var employees = from e in xml.Elements("employee")
                      where (string)e.Element("department") == "Sales"
                      select e;
      
      foreach (var employee in employees)
      {
          Console.WriteLine(employee.Element("name").Value);
      }
      
  4. LINQ to DataSet

    • Description - Used to query DataSets, typically obtained from databases or other data sources.

      Example
      DataSet ds = GetDataSet(); // Assume this method returns a populated DataSet
      var employees = from e in ds.Tables["Employees"].AsEnumerable()
                      where e.Field("Department") == "Sales"
                      select e;
      
      foreach (var employee in employees)
      {
          Console.WriteLine(employee.Field("Name"));
      }
      
  5. LINQ to Entities (Entity Framework)

    • Description -Used to query databases using the Entity Framework ORM, providing an abstraction over database tables.

      Example
      using (var context = new MyDbContext())
      {
          var employees = from e in context.Employees
                          where e.Department == "Sales"
                          select e;
      
          foreach (var employee in employees)
          {
              Console.WriteLine(employee.Name);
          }
      }

Prev Next