c# linq / lambda expressions - … · linq –standard query operators restriction where projection...

13
C# LINQ / Lambda Expressions © Meganadha Reddy K., 2015 http://www.netcomlearning.com/ [Introductory Session] Meganadha Reddy K. Technical Trainer | NetCom Learning www.NetComLearning.com

Upload: doxuyen

Post on 14-Feb-2019

276 views

Category:

Documents


3 download

TRANSCRIPT

C# LINQ / Lambda

Expressions

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

[Introductory Session]

Meganadha Reddy K.Technical Trainer | NetCom Learning

www.NetComLearning.com

Agenda

• Introduction to Collections: 10 Mins

• Normal way of using loops: 5 Mins

• var keyword : 5 Mins

• LINQ Examples : 20 Mins

• Lambda Expressions : 15 Mins

• Q & A : 5 Mins

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

This is L100 Webinar on “SQL Server Data Types”

Dot Net Framework

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Collections in C#

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

• Sample Collections:

a. ArrayList

b. Stack

c. Queue

• Sample Generics:

a. List<T>

b. Stack<T>

c. Queue<T>

Demos

Before LINQ

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

List<int> marks = new List<int>(){30,40,50,60,70,80,20};

foreach(int m in marks){

if(m>50){

Console.WriteLine(m);}

}

Traditional way of writing a for each loop

Queries without LINQ

foreach(Customer c in customers)if (c.Region == "USA") ...

Objects using loops and conditions

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Queries without LINQ

//Customers/Customer[@Region='USA']

XML using XPath/XQuery

SELECT * FROM Customers WHERE Region='USA'

SELECT from database tables

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Types of LINQ

• LINQ to Objects

• LINQ to SQL

• LINQ to XML

• LINQ to Entities

Our focus for now will be on LINQ to Objects

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

What is LINQ?

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Language INtegrated Query

List<int> marks = newList<int>{30,40,50,60,70,80,20};

var result =from m in markswhere m > 50select m;

var Keyword

LINQ Syntax

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

from id in source

{ from id in source | where condition }

[ orderby ordering, ordering, … ]

select expr | group expr by key

[ into id query ]

LINQ – Standard Query Operators

Restriction Where

Projection Select, SelectMany

Ordering OrderBy, ThenBy

Grouping GroupBy

Quantifiers Any, All

Partitioning Take, Skip, TakeWhile, SkipWhile

Sets Distinct, Union, Intersect, Except

Elements First, FirstOrDefault, ElementAt

Aggregation Count, Sum, Min, Max, Average

Conversion ToArray, ToList, ToDictionary

Casting OfType<T>

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

LINQ / Lambda - Demos

Demos

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/

Q & A

?

© Meganadha Reddy K., 2015 http://www.netcomlearning.com/