0% found this document useful (0 votes)
24 views33 pages

Where Linq Fits Into Your Toolbelt Slides

Uploaded by

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

Where Linq Fits Into Your Toolbelt Slides

Uploaded by

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

LINQ Fundamentals in C# 10

Where LINQ Fits into Your Toolbelt

Paul D. Sheriff
Business / IT Consultant

[email protected] www.pdsa.com
Version Check
This course was created by using:
- .NET 6
- C# 10
- Visual Studio Code 1
- Visual Studio 2022
Version Check
This course is 100% applicable to:
- .NET 6
- C# 10
- Visual Studio Code 1
- Visual Studio 2022
Advantages of using LINQ
Select and order data
Search for data
Course Goals
Extract subsets of data
What is in common within items in
collections
What is in common between collections
Join and group data
Aggregate data using Min(), Max(), Sum(),
etc.
Understand how deferred execution works
I assume you…
- Are a C# developer
- Are familiar with VS Code or Visual Studio
- New to using LINQ
Prerequisites
- C# Generics
- C# Delegates, Lambda Expressions
- C# Extension Methods
About This Course
What's in This Course

Learn LINQ query/method


Over 140 demos!
syntax side-by-side
How to Get the Most out of This Course

Watch this module


Download the Follow along with
for important LINQ
starting exercises the demos
basics
LINQ Community Resources

https://github.com/PaulDSheriff/LINQFundamentalsCSharp10

https://docs.microsoft.com/en-us/dotnet/csharp/programming-
guide/concepts/linq/

https://docs.microsoft.com/en-us/samples/dotnet/try-samples/101-
linq-samples/

https://blogs.pdsa.com - Search for LINQ


What Is LINQ?
What Is LINQ?

Query any type of collections


SQL-like syntax in C# and that implement
Visual Basic IEnumerable<T> or
IQueryable<T>
Common IEnumerable Types

Any array

String (Array of characters)

List<T> (Examples: List<Product>, List<Customer>)

HashSet<T>, Dictionary<TKey, TValue>, LinkedList<T>, etc.


LINQ Integrations (IQueryable)

LINQ to XML Entity Framework

XML LINQ Translator SQL LINQ Translator


LINQ Integrations (IQueryable)

LINQ to XML Entity Framework

Pluralsight Course:
Pluralsight Course:
Getting Started with
Working with XML in C#
Entity Framework 6
LINQ to Objects

LINQ and File


LINQ and Strings LINQ and Reflection
Directories

LINQ to Entities LINQ to DataSet


Using LINQ

Must add using statement Adds extension methods of


Enumerable and Queryable
using System.Linq; base classes
Examples of SQL, C# Loops, and LINQ
Comparison of SQL, Loops and LINQ

Let's look at SQL, looping and


SQL is very similar to LINQ
LINQ
Using a SQL Where Clause

SELECT * FROM Products


WHERE ListPrice > 1000
Simulate a SQL Where Clause Using C#
List<Product> products = GetProducts();

List<Product> list = new ();

foreach (Product product in products) {

if(product.ListPrice > 1000) {

list.Add(product);

C# LINQ Where Clause


List<Product> products = GetProducts();

var list = (from prod in products


where prod.ListPrice > 1000
select prod).ToList();
Using a SQL DISTINCT Clause

SELECT DISTINCT Color FROM Products


Simulate a SQL DISTINCT Clause Using C#
List<Product> products = GetProducts();

List<string> list = new();

foreach (Product product in products) {

if (!list.Contains(product.Color)) {

list.Add(product.Color);

C# LINQ Distinct() Method


List<Product> products = GetProducts();

var colors = (from prod in products


select prod.Color).Distinct().ToList()
Using a SQL MIN() Aggregate Function

SELECT MIN(ListPrice) FROM Products


Simulate SQL MIN() Using C#
List<Product> products = GetProducts();

decimal ret = decimal.MaxValue;

foreach (Product product in products) {

if (product.ListPrice < ret) {

ret = product.ListPrice;

C# LINQ Min() Method


List<Product> products = GetProducts();

decimal value = (from prod in products


select prod.ListPrice).Min();
SQL Query vs. LINQ Query Syntax

SQL LINQ

SELECT MAX(ListPrice) FROM (from prod in Products


Products select prod.ListPrice).Max()

SELECT AVG(ListPrice) FROM (from prod in Products


Products select prod.ListPrice).Average()
SQL Query vs. LINQ Query Syntax

SQL LINQ

SELECT * FROM Products from prod in Products


ORDER BY Name DESC orderby prod.Name descending
select prod

SELECT Name FROM Products from prod in Products


select prod.Name
Why Use LINQ?

Unified approach for querying


Eliminate looping code
any type of objects

Type-checking of objects at
IntelliSense support
compile time
What Can You Do With LINQ?
LINQ Operations

Order
Projection
Select (ascending /
(change shape)
descending)

Get an Element
Filter
(find, first, last,
(where)
single)
LINQ Operations

Iteration / Partioning Quantify


(foreach, skip, take) (any, all, contains)

Set Comparison Set Operations


(equal, except, intersection) (union, concat)
LINQ Operations

Joining Grouping
(inner joins, outer joins) (groupby, subquery, groupjoin)

Distinct Sets Aggregation


(distinct) (count, sum, min, max, average)
Module
Summary LINQ is a sql-like syntax for C#/Visual Basic
Can be used with many types of
collections
Can search, order, group, etc.
Can integrate with XML, databases
Up Next:
Use LINQ to Select Data within Collections

You might also like