0% found this document useful (0 votes)
43 views11 pages

LINQ Presentation With Examples

LINQ (Language Integrated Query) enables querying data directly in C# and unifies data handling across various sources like collections, SQL databases, and XML. It features both query and method syntax, supports operations such as filtering and sorting, and includes advanced features like deferred execution and anonymous types. Best practices suggest using method syntax for flexibility and minimizing collection enumerations to enhance performance.

Uploaded by

kahanikaar563200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views11 pages

LINQ Presentation With Examples

LINQ (Language Integrated Query) enables querying data directly in C# and unifies data handling across various sources like collections, SQL databases, and XML. It features both query and method syntax, supports operations such as filtering and sorting, and includes advanced features like deferred execution and anonymous types. Best practices suggest using method syntax for flexibility and minimizing collection enumerations to enhance performance.

Uploaded by

kahanikaar563200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

LINQ (Language Integrated

Query)
Understanding LINQ in .NET with
Examples
Introduction to LINQ
• • LINQ (Language Integrated Query) allows
querying data in C# directly.
• • It was introduced in C# to unify the way
developers work with data.
• • LINQ is similar to SQL but integrated within
the language.

• LINQ is mainly used to query:


• - Collections (LINQ to Objects)
LINQ Architecture
• • LINQ to Objects: Queries in-memory
collections like Lists, Arrays.
• • LINQ to SQL: Queries relational databases
using LINQ syntax.
• • LINQ to XML: Queries XML documents using
LINQ.
• • LINQ to Entities (Entity Framework): Works
with ORM frameworks like EF.
Basic LINQ Syntax
• • Query Syntax (SQL-like syntax)
• Example:
• var result = from x in numbers where x > 5
select x;

• • Method Syntax (Lambda expressions)


• Example:
• var result = numbers.Where(x => x >
5).Select(x => x);
LINQ Operations (Standard Query
Operators)
• • Filtering (Where):
• var filtered = numbers.Where(x => x > 10);

• • Sorting (OrderBy):
• var sorted = numbers.OrderBy(x => x);

• • Projection (Select):
• var projection = people.Select(p => p.Name);
Advanced LINQ Features
• • Deferred Execution: Queries are not
executed until results are enumerated.
• Example:
• var query = numbers.Where(x => x > 5); //
No execution yet
• foreach(var num in query) // Execution
happens here

• • Anonymous Types: Project data into new,


unnamed types.
LINQ to SQL vs SQL
• • LINQ to SQL translates LINQ queries into SQL
queries under the hood.
• • Example of SQL vs LINQ Query:
• SQL:
• SELECT * FROM Employees WHERE Age >
30;
• LINQ:
• var result = db.Employees.Where(e => e.Age
> 30);
Error Handling in LINQ
• • Use try-catch for handling exceptions in LINQ
queries.
• Example:
• try {
• var result = numbers.Where(x => x >
10).FirstOrDefault();
• } catch (Exception ex) {
• Console.WriteLine(ex.Message);
• }
Demo
• • Example: Querying a list of numbers
• var numbers = new List<int> { 1, 2, 3, 4, 5, 6,
7, 8, 9, 10 };
• var evenNumbers = numbers.Where(x => x %
2 == 0);
• foreach (var num in evenNumbers) {
• Console.WriteLine(num);
• }
• Output: 2, 4, 6, 8, 10
Best Practices in LINQ
• • Use method syntax for flexibility.
• • Avoid multiple enumerations of collections.
• • Use 'FirstOrDefault' or 'SingleOrDefault' to
avoid exceptions.
• • Example of 'FirstOrDefault':
• var first = numbers.FirstOrDefault(x => x > 5);
• if (first != null) { Console.WriteLine(first); }
Conclusion
• • LINQ is a powerful tool for querying data
in .NET.
• • It allows type-safe, readable queries across
multiple data sources.
• • Combining LINQ with C# features like
lambdas and anonymous types increases
flexibility and productivity.

You might also like