Understanding LINQLINQ in C#
MD. Shohag Mia
mdshohagmia14@outlook.com
.NET Trainee at LICT Project, World Bank
Index
• Introduction to LINQ
• Why should we use LINQ?
• LINQ architecture
• Lambda Expression
• Getting started with standard query operators
• LINQ to objects
• Getting started with LINQ to SQL integration
Introduction to LINQ
• LINQ stands for Language Integrated Query
• A query is an expression that retrieves data from a data source or database.
• Retrieve data from different data source like an object collection, sql server
database, xml & web service etc.
• Syntax like- var students = dbContext.Students.ToList();
Why should we use LINQ?
LINQ enables us to work with different data sources using similar types of code
styles like SQL Database, XML Documents and in-memory objects.
Another benefit of using LINQ is that it provides intellisense and compile time
error checking .
Advantage
• Quick turn around for development
• Queries can be dynamically generated
• Tables are automatically created into class
• Columns are automatically created into properties
• Relationship are automatically appeaded to classes
• Lambda expressions are awesome
• Data is easy to setup and use
• Easy debugging & transformation (SQL Data to XML Data)
Limitations
• There is an overhead for creating queries
• Small data sets will take longer to build the query than execute
• Table joins are very slow
• Hard to understand advance queries using Expressions
LINQ Architecture
Lambda Expression
C# 3.0(.NET 3.5) introduced the lambda expression along with LINQ. In C# a lambda is a function that
uses clear and short syntax like Parameters => Expression
Example: var customers = dbContext.Customers.Where(c => c.City == "London");
There are over 50 standard query operators available in LINQ that provide different functionalities like
filtering, sorting, grouping, aggregation, concatenation, etc.
Getting started with standard query operators
Standard Query Operators in Query Syntax
Three Parts of a Query Operation
LINQ to Object
• Native query syntax in C# , VB etc with IntelliSence Autocompletion.
• Query operator can be used against any .net collection (Innumerable<T>) such as Select, Where,
GroupBy, Join etc.
• Lamda Expression.
Getting started with LINQ to SQL integration
LINQ to SQL offers an infrastructure (run-time) for the management of relational data as objects. It is a
component of version 3.5 of the .NET Framework and ably does the translation of language-integrated
queries of the object model into SQL.
Example
Resources
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-with-
linq
• https://msdn.microsoft.com/en-us/library/bb308959.aspx
• https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/linq/
Any Question?
☺☺☺
♥♥♥Happy Coding♥♥♥

Understanding LINQ in C#

  • 1.
    Understanding LINQLINQ inC# MD. Shohag Mia mdshohagmia14@outlook.com .NET Trainee at LICT Project, World Bank
  • 2.
    Index • Introduction toLINQ • Why should we use LINQ? • LINQ architecture • Lambda Expression • Getting started with standard query operators • LINQ to objects • Getting started with LINQ to SQL integration
  • 3.
    Introduction to LINQ •LINQ stands for Language Integrated Query • A query is an expression that retrieves data from a data source or database. • Retrieve data from different data source like an object collection, sql server database, xml & web service etc. • Syntax like- var students = dbContext.Students.ToList();
  • 4.
    Why should weuse LINQ? LINQ enables us to work with different data sources using similar types of code styles like SQL Database, XML Documents and in-memory objects. Another benefit of using LINQ is that it provides intellisense and compile time error checking .
  • 5.
    Advantage • Quick turnaround for development • Queries can be dynamically generated • Tables are automatically created into class • Columns are automatically created into properties • Relationship are automatically appeaded to classes • Lambda expressions are awesome • Data is easy to setup and use • Easy debugging & transformation (SQL Data to XML Data)
  • 6.
    Limitations • There isan overhead for creating queries • Small data sets will take longer to build the query than execute • Table joins are very slow • Hard to understand advance queries using Expressions
  • 7.
  • 8.
    Lambda Expression C# 3.0(.NET3.5) introduced the lambda expression along with LINQ. In C# a lambda is a function that uses clear and short syntax like Parameters => Expression Example: var customers = dbContext.Customers.Where(c => c.City == "London");
  • 9.
    There are over50 standard query operators available in LINQ that provide different functionalities like filtering, sorting, grouping, aggregation, concatenation, etc. Getting started with standard query operators
  • 10.
    Standard Query Operatorsin Query Syntax
  • 11.
    Three Parts ofa Query Operation
  • 12.
    LINQ to Object •Native query syntax in C# , VB etc with IntelliSence Autocompletion. • Query operator can be used against any .net collection (Innumerable<T>) such as Select, Where, GroupBy, Join etc. • Lamda Expression.
  • 13.
    Getting started withLINQ to SQL integration LINQ to SQL offers an infrastructure (run-time) for the management of relational data as objects. It is a component of version 3.5 of the .NET Framework and ably does the translation of language-integrated queries of the object model into SQL.
  • 14.
  • 15.
  • 16.