Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to LINQ

What is LINQ?

LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to write queries directly within the C# language. It provides a consistent way to query various data sources such as collections, databases, XML, and more, using syntax that is integrated with the C# language.

Why Use LINQ?

LINQ offers several benefits for developers:

  • Improved readability and maintainability of code.
  • Reduction in the amount of code needed to perform queries.
  • Strongly typed queries with compile-time checking.
  • Consistency across different data sources.

Basic LINQ Syntax

LINQ queries are typically written using query syntax or method syntax. Here is an example of both:

Query Syntax

var numbers = new int[] { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
                  where num % 2 == 0
                  select num;
                

Method Syntax

var numbers = new int[] { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(num => num % 2 == 0);
                

Working with Collections

LINQ can be used to query collections such as arrays, lists, and other enumerable types. Here is an example:

Example

using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> fruits = new List<string> { "Apple", "Banana", "Mango", "Orange" };
        var filteredFruits = from fruit in fruits
                             where fruit.Contains("a")
                             select fruit;

        foreach(var fruit in filteredFruits)
        {
            Console.WriteLine(fruit);
        }
    }
}
                
Output:
Banana
Mango
Orange
                

LINQ to Objects

LINQ to Objects refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly in memory. Here is an example using an array:

Example

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] names = { "John", "Jane", "Joe", "Jill" };
        var jNames = from name in names
                     where name.StartsWith("J")
                     select name;

        foreach(var name in jNames)
        {
            Console.WriteLine(name);
        }
    }
}
                
Output:
John
Jane
Joe
Jill
                

LINQ to SQL

LINQ to SQL allows developers to query databases using LINQ syntax. It translates LINQ queries to SQL queries, and the results are returned as objects. Here is a basic example:

Example

// Assuming you have a database context named 'DataContext'
using (var context = new DataContext())
{
    var customers = from customer in context.Customers
                    where customer.City == "New York"
                    select customer;

    foreach (var customer in customers)
    {
        Console.WriteLine(customer.Name);
    }
}
                

Conclusion

LINQ is a versatile and powerful feature in C# that simplifies querying various data sources. Whether you are working with collections, XML, or databases, LINQ provides a consistent and efficient way to retrieve and manipulate data. By mastering LINQ, you can write more readable, maintainable, and efficient code.