📒
Notes
Cloud ComputingData Science/AIGame Development
  • Home
  • Big O
  • Data Structures & Algorithms
    • Data Structures
      • Array
      • Stack
      • Queue
      • Linked List
      • Binary Tree
    • Algorithms
      • Searching
      • Sorting
      • Graphs
        • Searching
        • Minimum Spanning Tree
        • Shortest Path Algorithms
      • String Algorithms
  • Object Oriented Programming
  • Languages
    • HTML/CSS
      • CSS
    • C++
    • C#
      • Types
      • Keywords
        • Modifiers
          • Access Modifiers
        • Method Parameters
      • Operators and Expressions
      • Collections
      • Constructors
      • Delegates
      • Indexers
      • Concepts
      • Features
        • LINQ
          • Operators
          • Working with Data
          • Methods
          • Resources
        • Asynchronous Programming
        • Reflection
    • Dart
    • GraphQL
    • JavaScript
      • Variable and Parameter
      • Built-in objects
        • Array
        • Built-in Functions
      • Functions
      • Classes
      • Prototype
      • Libraries
        • jQuery
        • React
          • Components
          • State and Lifecycle
          • Hooks
            • useState
            • useEffect
          • Resources
      • Testing Framework
      • Web APIs
    • Kotlin
      • Basics
    • Python
      • Basics
      • Data Structures
      • Functions
      • Resources
        • Flask
    • SQL
      • Basics
      • Operators
      • JOINs
      • Aggregations
      • Subqueries
      • Views
      • Functions
        • Window Functions
      • Stored Procedures
      • Performance Tuning
      • Extras
    • Resources
  • 🌐Web Frameworks
    • Angular
      • Templates
      • Directives
        • Attribute Directives
        • Structural Directives
    • ASP.NET
      • Fundamentals
        • Dependency Injection
        • Middleware
        • Session & State Management
      • Web apps
        • MVC
          • Controllers
            • Filters
          • Models
            • Model Binding
            • Model Validation
          • Views
            • Tag Helpers
            • View Components
          • Features
        • Client-side development
      • Web APIs
        • Controller-based APIs
        • Minimal APIs
        • OpenAPI
        • Content Negotiation
      • SignalR
      • Host and Deploy
        • IIS
      • Security
    • Django
      • The Request/Response Cycle
    • Terminologies
      • Web Server
        • Internet Information Services
    • Resources
  • 📱App Frameworks
    • Introduction
      • Resources
    • Xamarin
      • Lifecycle
      • Custom Renderers & Effects
      • Behaviors
      • Triggers
      • Gestures
      • Commands
      • Dependency Service in XF
      • Libraries
      • Showcase
    • .NET MAUI
      • Controls
      • Navigation
      • Storage Options
  • Multi-Platform Frameworks
    • .NET
      • .NET Framework
        • ADO.NET
        • WCF
      • Fundamentals
        • Logging
        • Testing
      • Advanced
        • Asynchronous Programming
        • Parallel Programming
        • Threading
        • Memory Management
          • Garbage Collection
    • Flutter
  • Object-Relational Mappers
    • Entity Framework
      • Application Models
      • Configuration
      • Setting Up
      • Advanced
  • Databases
    • Introduction
      • DBMS Architecture
      • Normalization
      • Database Transaction Models
    • Relational Databases
      • Microsoft SQL Server
        • Basics
        • Functions
        • Stored Procedures
        • Error Handling
        • Log Shipping
        • Querying and Manipulating JSON data
        • Statements
        • Topics
        • Extras
    • Non-Relational Databases
      • MongoDB
      • Redis
        • Data Structures
        • Introduction
        • Managing Database
  • Tools
    • Version Control
      • Git
        • Setup and Config
        • Basics
          • Sharing and Updating Projects
        • Resources
      • Perforce Helix
    • GitHub
    • Powershell
  • Software Development
    • Software Development Life Cycle
    • Software Design Patterns
      • GoF Design Patterns
      • Architectural Patterns
        • MVC
        • MVVM
        • N-tier Architecture
        • Onion Architecture
        • Data Transfer Objects
      • CQRS
    • Software Design Principles
      • S.O.L.I.D. Priniciple
  • System Design
    • Topics
      • Load Balancing
  • Topics
    • JWT
    • Caching
      • Static vs Dynamic Caching
    • OSI model
      • HTTP
    • Glossary
    • API
      • SOAP
      • REST
    • Microservices
    • WebHooks
    • Practice
    • Operating Systems
      • Windows
    • Architecture
  • 🔖Bookmarks
  • 🔗Resources
Powered by GitBook
On this page
  • Advantages of LINQ
  • Lambda Expression

Was this helpful?

  1. Languages
  2. C#
  3. Features

LINQ

Language-Integrated Query

PreviousFeaturesNextOperators

Last updated 1 year ago

Was this helpful?

The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).

  • LINQ is a uniform structured query syntax built in C# and VB.NET to retrieve data from different types of data sources and formats such as collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server, and other databases.

LINQ queries return results as objects. It enables you to uses object-oriented approach on the result set and not to worry about transforming different formats of results into objects.

Advantages of LINQ

  • Familiar language: Developers don’t have to learn a new query language for each type of data source or data format.

  • Less coding: It reduces the amount of code to be written as compared with a more traditional approach.

  • Readable code: LINQ makes the code more readable so other developers can easily understand and maintain it.

  • Standardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources.

  • Compile time safety of queries: It provides type checking of objects at compile time.

  • IntelliSense Support: LINQ provides IntelliSense for generic collections.

  • Shaping data: You can retrieve data in different shapes.

LINQ queries uses extension methods for classes that implement IEnumerable or IQueryable interface. The Enumerable and Queryable are two static classes that contain extension methods to write LINQ queries.

Points to Remember:

  1. Use System.Linq namespace to use LINQ.

  2. LINQ api includes two main static class Enumerable & Queryable.

  3. The static Enumerable class includes extension methods for classes that implements the IEnumerable<T> interface.

  4. IEnumerable<T> type of collections are in-memory collection like List, Dictionary, SortedList, Queue, HashSet, LinkedList.

  5. The static Queryable class includes extension methods for classes that implements the IQueryable<T> interface.

  6. Remote query provider implements e.g. Linq-to-SQL, LINQ-to-Amazon etc.

Syntax
  • Query Syntax Example

var countries = new string[] { "India", "Japan", "USA", "UK" };

var result = from country in countries
                where country.Contains("a")
                select country;
  • Method Syntax Example

var countries = new List<string>() { "India", "Japan", "USA", "UK" };

var result = countries.Where(c => c.Contains('a'));
output
foreach(var i in result){
    Console.WriteLine(i);
}

/* 
India
Japan  
*/

Lambda Expression

Anonymous Method in C#
delegate(Student s) { return s.Age > 12 && s.Age < 20; };
Lambda Expression in C#
s => s.Age > 12 && s.Age < 20

Q. Explain how LINQ is useful than Stored Procedures?

  • Debugging: It is difficult to debug a stored procedure but as LINQ is part of.NET, visual studios debugger can be used to debug the queries

  • Deployment: For stored procedure, additional script should be provided but with LINQ everything gets compiled into single DLL hence deployment becomes easy

  • Type Safety: LINQ is type safe, so queries errors are type checked at compile time

System.Data.Dlinq.dll provides the functionality to work with LINQ to SQL

Q. Explain what is lambda expressions in LINQ? Lambda expression is referred to as a unique function use to form delegates or expression tree types, where right side is the output and left side is the input to the method. For writing LINQ queries particularly, Lambda expression is used.

Q. Mention what is the role of DataContext classes in LINQ? DataContext class acts as a bridge between database and the LINQ to SQL. For accessing the database and also for changing the data in the database, it contains connections string and the functions.

SQL Server
LogoLanguage Integrated Query (LINQ) in C# - C#MicrosoftLearn