SlideShare a Scribd company logo
1 of 27
Language INtegrated Query 
Before “Hello LINQ!” , 
There is “Why LINQ ?!” 
WHY 
LINQ?!
HELLO 
LINQ!
Var Keyword 
Extension Methods 
Lambda Expression 
LINQ 
BASICS
var text = "Hello LINQ!“ ; 
System.Col lect ions.Generic. IEnumerable<bool> 
class Ful l_of_Er rors 
{ 
var s = 9; 
publ ic var MyMethod( ) 
{ 
} 
publ ic void MyMethod2(var t ) 
{ 
} 
} 
VAR 
KEYWORD
publ ic stat ic class St r ingExtensions 
{ 
publ ic stat ic int Conver tToInt ( this st r ing number ) 
{ 
return Int32.Parse(number ) ; 
} 
publ ic stat ic st r ing HammooD( this f loat number ) 
{ 
return number.ToSt r ing( ) ; 
} 
} 
EXTENSION 
METHODS
LAMBDA EXPRESSION 
str ing[ ] names = new str ing[ ] { "Hammod", "Tarek", "Alyan", "Nawwar" } ; 
var name = names.Select (s => s.Star tsWi th("A") ) ;
SIMPLE EXAMPLES 
int[] fibNum = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; 
double averageValue = fibNum.Where(num => num % 2 == 1).Average( ) ;
var resul t = from b in Books 
where b.Pr ice > 50.00 
select b; 
foreach (Book book in resul t ) { 
Console.Wr i teLine(book.name) 
} 
SIMPLE EXAMPLES
int [ ] numbers = new int [7] { 0, 1, 2, 3, 4, 5, 6 }; 
var numQuery = from num in numbers 
where (num % 2) == 0 
select num; 
foreach ( int num in numQuery) { 
Console.Write("{0} ", num); 
} 
SIMPLE EXAMPLES
SIMPLE EXAMPLES 
 First() 
 FirstOrDefault() 
 Single() 
 SingleOrDefault() 
 Al l () 
 Any() 
 Sum( ) 
 Contains( ) 
 ……
SIMPLE EXAMPLES 
int [ ] numbers = new int [7] { 0, 1, 2, 3, 4, 5, 6 }; 
var numQuery = ( f rom num in numbers 
where (num % 9) == 0 
select num); 
i f (numQuery.Any()) 
{ 
foreach ( int num in numQuery) 
{ 
Console.Wr ite("{0} ", num); 
} 
} 
//more ideas?
LINQ TO XML – SIMPLE FILE 
<?xml version="1.0"?> 
<BookParticipants> 
<BookParticipant type="Author"> 
<FirstName>Joe</FirstName> 
<LastName>Rattz</LastName> 
</BookParticipant> 
<BookParticipant type="Editor"> 
<FirstName>Ewan</FirstName> 
<LastName>Buckingham</LastName> 
</BookParticipant> 
</BookParticipants>
LINQ TO XML – DOM SYNTAX 
Xml E l ement xmlBo o kPar t i c ipa nt ; 
XmlAt t r ibute xmlPa r t i c ipant T ype ; 
Xml E l ement xml F i r s tName ; 
Xml E l ement xml L a s tName ; 
XmlDo cume nt xmlDo c = n ew XmlDo c ument ( ) ; 
Xml E l ement xmlBo o kPar t i c ipa nt s = xmlDo c .Cr ea t eEl emen t ( "Bo o kPar t i c ipa nt s " ) ; 
xmlDo c .AppendChi ld ( xmlBo o kPar t i c ipant s ) ; 
xmlBo o kPar t i c ipa nt = xmlDo c .Cr e at eE l eme nt ( "Bo o kPar t i c ipa nt " ) ; 
xmlPar t i c ipa nt T ype = xmlDo c .Cr e at eAt t r ibut e ( " t ype " ) ; 
xmlPar t i c ipa nt T ype. In ner T e x t = "Au tho r " ; 
xmlBoo kPar t i c ipa nt .At t r ibute s .Append ( xmlPar t i c ipa nt T ype ) ; 
xml F i r s tName = xmlDo c .Cr e at eE l eme nt ( " F i r s tName " ) ; 
xml F i r s tName. Inn er T e x t = " J o e " ; 
xmlBo o kPar t i c ipa nt .Appe ndCh i ld ( xml F i r s tName ) ; 
xml L as tName = xmlDo c .Cr e at eE l eme nt ( " Las tName " ) ; 
xml L as tName. In ner T e x t = "Ra t t z " ; 
xmlBo o kPar t i c ipa nt .Appe ndCh i ld ( xml La s tName ) ; 
xmlBo o kPar t i c ipa nt s .Appe ndCh i ld ( xmlBo o kPar t i c ipa nt ) ; 
xmlBoo kPar t i c ipa nt = xmlDo c .Cr e at eEl eme nt ( "Boo kPar t i c ipa nt " ) ; 
xmlPar t i c ipa nt T ype = xmlDo c .Cr e at eAt t r ibut e ( " t ype " ) ; 
xmlPar t i c ipa nt T ype. In ner T e x t = " E d i t o r " ; 
xmlBo o kPar t i c ipa nt .At t r ibute s .Append ( xmlPar t i c ipa nt T ype ) ; 
xml F i r s tName = xmlDo c .Cr e at eE l eme nt ( " F i r s tName " ) ; 
xml F i r s tName. Inn er T e x t = " Ewa n" ; 
xmlBo o kPar t i c ipa nt .Appe ndCh i ld ( xml F i r s tName ) ; 
xml L as tName = xmlDo c .Cr e at eE l eme nt ( " Las tName " ) ; 
xml L as tName. In ner T e x t = "Buc ki ngh am" ; 
xmlBoo kPar t i c ipa nt .Appe ndCh i ld ( xml La s tName ) ; 
xmlBo o kPar t i c ipa nt s .Appe ndCh i ld ( xmlBo o kPar t i c ipa nt ) ;
LINQ TO XML – BUILDING AN ELEMENT 
var xBookParticipants = 
new XElement("BookParticipants", 
new XElement("BookParticipant", 
new XAttribute("type", "Author"), 
new XElement("FirstName", "Joe"), 
new XElement("LastName", "Rattz")), 
new XElement("BookParticipant", 
new XAttribute("type", "Editor"), 
new XElement("FirstName", "Ewan"), 
new XElement("LastName", 
"Buckingham")));
LINQ TO XML - METHODS 
doc.Save(@"E:fi le.xml") ; 
doc.Save(@"E:fi le.xml",SaveOptions.DisableFormatt ing) ; 
XDocument xmlDoc1 = XDocument .Load("TestFi le.xml") ; 
XElement name = new XElement ("Author", "Hammod") ; 
Console.Wr i teLine(name.Value) ;
LINQ TO XML - XELEMENT 
str ing[ ] names = { "M.Hammod", "Golden Man", "Mohammed_807", 
"Nawwar" } ; 
XElement OurGroupMembers = new XElement("OurGroup", 
from n in names 
select new XElement("Name",n) ) ; 
Console.Wr i teLine(OurGroupMembers);
LINQ TO XML - XSTREAMING 
XStreamingElement OurGroupMembers = new 
XStreamingElement("OurGroup", 
from n in names 
select new XElement("Name", n)); 
names[3] = "Tamer"; 
Console.WriteLine(OurGroupMembers); 
names[2] = "Smer"; 
Console.WriteLine(OurGroupMembers); 
XStreamingElement VS. XElement
LINQ TO XML - XSTREAMING 
<OurGroup> 
<Name>M.Hammod</Name> 
<Name>Golden Man</Name> 
<Name>Mohammed_807</Name> 
<Name>Tamer</Name> 
</OurGroup> 
<OurGroup> 
<Name>M.Hammod</Name> 
<Name>Golden Man</Name> 
<Name>Smer</Name> 
<Name>Tamer</Name> 
</OurGroup>
LINQ TO XML – ADDING ELEMENTS 
xDocument1.Element("BookParticipants").Add( 
new XElement("BookParticipant", 
new XAttribute("type", "Editor"), 
new XElement("FirstName", "Mohammad"), 
new XElement("LastName", "_807"))); 
- AddFi rst . 
- AddBeforeSel f . 
- AddAf terSel f .
LINQ TO XML – ADDING ELEMENTS 
xDocument1.Element("BookParticipants").Elements("BookParticipant"). 
Where(e => ((string)e.Element("FirstName")) =="Tarek"). 
Single<XElement>().AddBeforeSelf( 
new XElement("BookParticipant", 
new XAttribute("type", "Technical Reviewer"), 
new XElement("FirstName", "Nawwar"), 
new XElement("LastName", "soso")));
XElement elementToUpdate = xDocument1.Element("BookParticipants"). 
Elements("BookParticipant"). 
Single(e=>((string)e.Element("FirstName")) == "Tarek"); 
//using Value to Update 
elementToUpdate.Element("FirstName").Value = "New_Tarek"; 
- ReplaceAll 
LINQ TO XML - UPDATING
LINQ TO XML – REMOVE ELEMENTS 
- elementToRemove.Remove(); 
- xDocument1.Descendants( ) .Where(e => e.Name =="Fi rstName" ) .Remove( ) ; 
- xDocument1.Element ("BookPar t icipants" ) .RemoveAl l ( ) ;
LINQ TO SQL 
Linq To Sql Demo
LINQ – FUNC AND DELEGATES 
publ ic stat ic IEnumerable<TResul t> Select<TSource, TResul t>(this 
IEnumerable<TSource> source, Func<TSource, TResul t> selector ) ; 
publ ic delegate st r ing MyDelegate( int index) ; 
publ ic stat ic st r ing ReturnName( int index) 
{ 
st r ing[ ] names3 = new st r ing[ ] { "Hammod", "Tarek", "Alyan", "Nawwar" } ; 
return names3[ index] ; 
} 
stat ic MyDelegate SDelegate = new MyDelegate(ReturnName) ; 
Console.Wr i teLine(SDelegate(1) ) ;
LINQ – FUNC AND DELEGATES 
Mohammad AL- Hammod. 
m-khaled89@hotmai l .com 
Damascus – 2011. 
Deep Thanks

More Related Content

What's hot

Google Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidGoogle Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidJordi Gerona
 
Collectors in the Wild
Collectors in the WildCollectors in the Wild
Collectors in the WildJosé Paumard
 
Dumping Perl 6 (AmsterdamX.pm)
Dumping Perl 6 (AmsterdamX.pm)Dumping Perl 6 (AmsterdamX.pm)
Dumping Perl 6 (AmsterdamX.pm)brian d foy
 
Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)brian d foy
 
GoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDDGoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDDBartłomiej Kiełbasa
 
Ramda, a functional JavaScript library
Ramda, a functional JavaScript libraryRamda, a functional JavaScript library
Ramda, a functional JavaScript libraryDerek Willian Stavis
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner codeMite Mitreski
 
PrettyDump Perl 6 (London.pm)
PrettyDump Perl 6 (London.pm)PrettyDump Perl 6 (London.pm)
PrettyDump Perl 6 (London.pm)brian d foy
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingHenri Tremblay
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorFedor Lavrentyev
 
The core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaThe core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaMite Mitreski
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to GroovyAnton Arhipov
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorFedor Lavrentyev
 
Clean code with google guava jee conf
Clean code with google guava jee confClean code with google guava jee conf
Clean code with google guava jee confIgor Anishchenko
 

What's hot (20)

Google Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & AndroidGoogle Guava - Core libraries for Java & Android
Google Guava - Core libraries for Java & Android
 
What's new in Liferay Mobile SDK 2.0 for Android
What's new in Liferay Mobile SDK 2.0 for AndroidWhat's new in Liferay Mobile SDK 2.0 for Android
What's new in Liferay Mobile SDK 2.0 for Android
 
Java 8 Examples
Java 8 ExamplesJava 8 Examples
Java 8 Examples
 
Collectors in the Wild
Collectors in the WildCollectors in the Wild
Collectors in the Wild
 
Dumping Perl 6 (AmsterdamX.pm)
Dumping Perl 6 (AmsterdamX.pm)Dumping Perl 6 (AmsterdamX.pm)
Dumping Perl 6 (AmsterdamX.pm)
 
Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)Dumping Perl 6 (French Perl Workshop)
Dumping Perl 6 (French Perl Workshop)
 
GoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDDGoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDD
 
Ramda, a functional JavaScript library
Ramda, a functional JavaScript libraryRamda, a functional JavaScript library
Ramda, a functional JavaScript library
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
 
Google guava
Google guavaGoogle guava
Google guava
 
Google Guava for cleaner code
Google Guava for cleaner codeGoogle Guava for cleaner code
Google Guava for cleaner code
 
PrettyDump Perl 6 (London.pm)
PrettyDump Perl 6 (London.pm)PrettyDump Perl 6 (London.pm)
PrettyDump Perl 6 (London.pm)
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev FedorProgramming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
 
The core libraries you always wanted - Google Guava
The core libraries you always wanted - Google GuavaThe core libraries you always wanted - Google Guava
The core libraries you always wanted - Google Guava
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Clean code with google guava jee conf
Clean code with google guava jee confClean code with google guava jee conf
Clean code with google guava jee conf
 

Viewers also liked

أقوى برنامج محاسبة للاندرويد
أقوى برنامج محاسبة للاندرويدأقوى برنامج محاسبة للاندرويد
أقوى برنامج محاسبة للاندرويدalmanara web
 
Prince2_practitioner_certification_GCI
Prince2_practitioner_certification_GCIPrince2_practitioner_certification_GCI
Prince2_practitioner_certification_GCIClaudia Ganea
 
برنامج محاسبة للمطاعم
برنامج محاسبة للمطاعمبرنامج محاسبة للمطاعم
برنامج محاسبة للمطاعمalmanara web
 
الفصل الرابع مقاييس النزعة المركزية
الفصل الرابع مقاييس النزعة المركزيةالفصل الرابع مقاييس النزعة المركزية
الفصل الرابع مقاييس النزعة المركزيةOsama Hosam
 
استعدادات السفر
استعدادات السفراستعدادات السفر
استعدادات السفرWannie afifah
 
دبلومة معايير المحاسبة الدولية
دبلومة معايير المحاسبة الدوليةدبلومة معايير المحاسبة الدولية
دبلومة معايير المحاسبة الدوليةGlobal Training Academy
 
Blue ocean strategy arabic
Blue ocean strategy arabicBlue ocean strategy arabic
Blue ocean strategy arabicMohammad Alyan
 
دورة برنامج دروبوكس
دورة برنامج دروبوكسدورة برنامج دروبوكس
دورة برنامج دروبوكسEbteesam Al-amr
 
حماية المستهلك الالكتروني في القانون الدولي الخاص
حماية المستهلك الالكتروني في القانون الدولي الخاصحماية المستهلك الالكتروني في القانون الدولي الخاص
حماية المستهلك الالكتروني في القانون الدولي الخاصmohamed alhassani
 
التدريبات العملية لمحاسبة المقاولات المتخصصة
التدريبات العملية لمحاسبة المقاولات المتخصصةالتدريبات العملية لمحاسبة المقاولات المتخصصة
التدريبات العملية لمحاسبة المقاولات المتخصصةبرنامج محاسبة Erp System
 
نظام التعليم في فنلندا
نظام التعليم في فنلندانظام التعليم في فنلندا
نظام التعليم في فنلنداآمنة حامد
 
محاسبة الشركات 2 - اللقاء الافتراضي الاول
محاسبة الشركات 2 - اللقاء الافتراضي الاولمحاسبة الشركات 2 - اللقاء الافتراضي الاول
محاسبة الشركات 2 - اللقاء الافتراضي الاولجامعة القدس المفتوحة
 
التعليم والتوجيه المهني في المانيا
التعليم والتوجيه المهني في المانياالتعليم والتوجيه المهني في المانيا
التعليم والتوجيه المهني في المانياNazek Al-Asfoor
 
مهارات التفوق الدراسي (1)
مهارات التفوق الدراسي (1)مهارات التفوق الدراسي (1)
مهارات التفوق الدراسي (1)Mrs.Imanali
 
إدرس بذكاء وليس بجهد
إدرس بذكاء وليس بجهد إدرس بذكاء وليس بجهد
إدرس بذكاء وليس بجهد Younes Rajji
 

Viewers also liked (20)

أقوى برنامج محاسبة للاندرويد
أقوى برنامج محاسبة للاندرويدأقوى برنامج محاسبة للاندرويد
أقوى برنامج محاسبة للاندرويد
 
محاسبة المقاولات
محاسبة المقاولاتمحاسبة المقاولات
محاسبة المقاولات
 
المحاسبة لغير المحاسبين
المحاسبة لغير المحاسبينالمحاسبة لغير المحاسبين
المحاسبة لغير المحاسبين
 
Prince2_practitioner_certification_GCI
Prince2_practitioner_certification_GCIPrince2_practitioner_certification_GCI
Prince2_practitioner_certification_GCI
 
برنامج محاسبة للمطاعم
برنامج محاسبة للمطاعمبرنامج محاسبة للمطاعم
برنامج محاسبة للمطاعم
 
Formación profesorado
Formación profesoradoFormación profesorado
Formación profesorado
 
الفصل الرابع مقاييس النزعة المركزية
الفصل الرابع مقاييس النزعة المركزيةالفصل الرابع مقاييس النزعة المركزية
الفصل الرابع مقاييس النزعة المركزية
 
محاسبة المقاولات
محاسبة المقاولاتمحاسبة المقاولات
محاسبة المقاولات
 
استعدادات السفر
استعدادات السفراستعدادات السفر
استعدادات السفر
 
دبلومة معايير المحاسبة الدولية
دبلومة معايير المحاسبة الدوليةدبلومة معايير المحاسبة الدولية
دبلومة معايير المحاسبة الدولية
 
برنامج محاسبة التكاليف والتصنيع
برنامج محاسبة التكاليف والتصنيعبرنامج محاسبة التكاليف والتصنيع
برنامج محاسبة التكاليف والتصنيع
 
Blue ocean strategy arabic
Blue ocean strategy arabicBlue ocean strategy arabic
Blue ocean strategy arabic
 
دورة برنامج دروبوكس
دورة برنامج دروبوكسدورة برنامج دروبوكس
دورة برنامج دروبوكس
 
حماية المستهلك الالكتروني في القانون الدولي الخاص
حماية المستهلك الالكتروني في القانون الدولي الخاصحماية المستهلك الالكتروني في القانون الدولي الخاص
حماية المستهلك الالكتروني في القانون الدولي الخاص
 
التدريبات العملية لمحاسبة المقاولات المتخصصة
التدريبات العملية لمحاسبة المقاولات المتخصصةالتدريبات العملية لمحاسبة المقاولات المتخصصة
التدريبات العملية لمحاسبة المقاولات المتخصصة
 
نظام التعليم في فنلندا
نظام التعليم في فنلندانظام التعليم في فنلندا
نظام التعليم في فنلندا
 
محاسبة الشركات 2 - اللقاء الافتراضي الاول
محاسبة الشركات 2 - اللقاء الافتراضي الاولمحاسبة الشركات 2 - اللقاء الافتراضي الاول
محاسبة الشركات 2 - اللقاء الافتراضي الاول
 
التعليم والتوجيه المهني في المانيا
التعليم والتوجيه المهني في المانياالتعليم والتوجيه المهني في المانيا
التعليم والتوجيه المهني في المانيا
 
مهارات التفوق الدراسي (1)
مهارات التفوق الدراسي (1)مهارات التفوق الدراسي (1)
مهارات التفوق الدراسي (1)
 
إدرس بذكاء وليس بجهد
إدرس بذكاء وليس بجهد إدرس بذكاء وليس بجهد
إدرس بذكاء وليس بجهد
 

Similar to Linq introduction

Lambda expressions
Lambda expressionsLambda expressions
Lambda expressionsYuriy Seniuk
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationJoni
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
FUNctional Programming in Java 8
FUNctional Programming in Java 8FUNctional Programming in Java 8
FUNctional Programming in Java 8Richard Walker
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasGanesh Samarthyam
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
UNIVERSIDAD CENTRAL DEL ECUADOR C++
UNIVERSIDAD CENTRAL DEL ECUADOR C++UNIVERSIDAD CENTRAL DEL ECUADOR C++
UNIVERSIDAD CENTRAL DEL ECUADOR C++CamiEscobar1995
 
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++CamiEscobar1995
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Bryan O'Sullivan
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problemecomputernotes
 
Nik Graf - Get started with Reason and ReasonReact
Nik Graf - Get started with Reason and ReasonReactNik Graf - Get started with Reason and ReasonReact
Nik Graf - Get started with Reason and ReasonReactOdessaJS Conf
 

Similar to Linq introduction (20)

Lambda expressions
Lambda expressionsLambda expressions
Lambda expressions
 
Tips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET ApplicationTips and Tricks of Developing .NET Application
Tips and Tricks of Developing .NET Application
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
FUNctional Programming in Java 8
FUNctional Programming in Java 8FUNctional Programming in Java 8
FUNctional Programming in Java 8
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
 
More on Lex
More on LexMore on Lex
More on Lex
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java Beagle
Java BeagleJava Beagle
Java Beagle
 
UNIVERSIDAD CENTRAL DEL ECUADOR C++
UNIVERSIDAD CENTRAL DEL ECUADOR C++UNIVERSIDAD CENTRAL DEL ECUADOR C++
UNIVERSIDAD CENTRAL DEL ECUADOR C++
 
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
UNIVERSIDAD CENTRAL DEL ECUADOR CAMILA ESCOBAR LOPEZ C+++
 
C++
C++C++
C++
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Nik Graf - Get started with Reason and ReasonReact
Nik Graf - Get started with Reason and ReasonReactNik Graf - Get started with Reason and ReasonReact
Nik Graf - Get started with Reason and ReasonReact
 

More from Mohammad Alyan

More from Mohammad Alyan (20)

Apple case study
Apple case studyApple case study
Apple case study
 
Introduction to Industry Life Cycles
 Introduction to Industry Life Cycles  Introduction to Industry Life Cycles
Introduction to Industry Life Cycles
 
Crowd funding
Crowd fundingCrowd funding
Crowd funding
 
Coursera
CourseraCoursera
Coursera
 
Course index
Course indexCourse index
Course index
 
التفكير- مصنع للإبداع
التفكير- مصنع للإبداعالتفكير- مصنع للإبداع
التفكير- مصنع للإبداع
 
Cloud computing
Cloud computing Cloud computing
Cloud computing
 
Introduction To ERP
Introduction To ERPIntroduction To ERP
Introduction To ERP
 
10 system.security.cryptography
10 system.security.cryptography10 system.security.cryptography
10 system.security.cryptography
 
10 1 otp all
10 1 otp all10 1 otp all
10 1 otp all
 
9 networking
9 networking9 networking
9 networking
 
8 memory managment & pointers
8 memory managment & pointers8 memory managment & pointers
8 memory managment & pointers
 
7 multi threading
7 multi threading7 multi threading
7 multi threading
 
6 ado.net
6 ado.net6 ado.net
6 ado.net
 
5 fifth lesson -xml
5 fifth lesson -xml5 fifth lesson -xml
5 fifth lesson -xml
 
4 fourth lesson-deployment
4 fourth lesson-deployment4 fourth lesson-deployment
4 fourth lesson-deployment
 
3 third lesson-reflection
3 third lesson-reflection3 third lesson-reflection
3 third lesson-reflection
 
2 second lesson- attributes
2 second lesson- attributes2 second lesson- attributes
2 second lesson- attributes
 
1 first lesson -assemblies
1  first lesson -assemblies1  first lesson -assemblies
1 first lesson -assemblies
 
Coursera
CourseraCoursera
Coursera
 

Linq introduction

  • 1. Language INtegrated Query Before “Hello LINQ!” , There is “Why LINQ ?!” WHY LINQ?!
  • 3. Var Keyword Extension Methods Lambda Expression LINQ BASICS
  • 4. var text = "Hello LINQ!“ ; System.Col lect ions.Generic. IEnumerable<bool> class Ful l_of_Er rors { var s = 9; publ ic var MyMethod( ) { } publ ic void MyMethod2(var t ) { } } VAR KEYWORD
  • 5. publ ic stat ic class St r ingExtensions { publ ic stat ic int Conver tToInt ( this st r ing number ) { return Int32.Parse(number ) ; } publ ic stat ic st r ing HammooD( this f loat number ) { return number.ToSt r ing( ) ; } } EXTENSION METHODS
  • 6.
  • 7. LAMBDA EXPRESSION str ing[ ] names = new str ing[ ] { "Hammod", "Tarek", "Alyan", "Nawwar" } ; var name = names.Select (s => s.Star tsWi th("A") ) ;
  • 8. SIMPLE EXAMPLES int[] fibNum = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; double averageValue = fibNum.Where(num => num % 2 == 1).Average( ) ;
  • 9. var resul t = from b in Books where b.Pr ice > 50.00 select b; foreach (Book book in resul t ) { Console.Wr i teLine(book.name) } SIMPLE EXAMPLES
  • 10.
  • 11. int [ ] numbers = new int [7] { 0, 1, 2, 3, 4, 5, 6 }; var numQuery = from num in numbers where (num % 2) == 0 select num; foreach ( int num in numQuery) { Console.Write("{0} ", num); } SIMPLE EXAMPLES
  • 12. SIMPLE EXAMPLES  First()  FirstOrDefault()  Single()  SingleOrDefault()  Al l ()  Any()  Sum( )  Contains( )  ……
  • 13. SIMPLE EXAMPLES int [ ] numbers = new int [7] { 0, 1, 2, 3, 4, 5, 6 }; var numQuery = ( f rom num in numbers where (num % 9) == 0 select num); i f (numQuery.Any()) { foreach ( int num in numQuery) { Console.Wr ite("{0} ", num); } } //more ideas?
  • 14. LINQ TO XML – SIMPLE FILE <?xml version="1.0"?> <BookParticipants> <BookParticipant type="Author"> <FirstName>Joe</FirstName> <LastName>Rattz</LastName> </BookParticipant> <BookParticipant type="Editor"> <FirstName>Ewan</FirstName> <LastName>Buckingham</LastName> </BookParticipant> </BookParticipants>
  • 15. LINQ TO XML – DOM SYNTAX Xml E l ement xmlBo o kPar t i c ipa nt ; XmlAt t r ibute xmlPa r t i c ipant T ype ; Xml E l ement xml F i r s tName ; Xml E l ement xml L a s tName ; XmlDo cume nt xmlDo c = n ew XmlDo c ument ( ) ; Xml E l ement xmlBo o kPar t i c ipa nt s = xmlDo c .Cr ea t eEl emen t ( "Bo o kPar t i c ipa nt s " ) ; xmlDo c .AppendChi ld ( xmlBo o kPar t i c ipant s ) ; xmlBo o kPar t i c ipa nt = xmlDo c .Cr e at eE l eme nt ( "Bo o kPar t i c ipa nt " ) ; xmlPar t i c ipa nt T ype = xmlDo c .Cr e at eAt t r ibut e ( " t ype " ) ; xmlPar t i c ipa nt T ype. In ner T e x t = "Au tho r " ; xmlBoo kPar t i c ipa nt .At t r ibute s .Append ( xmlPar t i c ipa nt T ype ) ; xml F i r s tName = xmlDo c .Cr e at eE l eme nt ( " F i r s tName " ) ; xml F i r s tName. Inn er T e x t = " J o e " ; xmlBo o kPar t i c ipa nt .Appe ndCh i ld ( xml F i r s tName ) ; xml L as tName = xmlDo c .Cr e at eE l eme nt ( " Las tName " ) ; xml L as tName. In ner T e x t = "Ra t t z " ; xmlBo o kPar t i c ipa nt .Appe ndCh i ld ( xml La s tName ) ; xmlBo o kPar t i c ipa nt s .Appe ndCh i ld ( xmlBo o kPar t i c ipa nt ) ; xmlBoo kPar t i c ipa nt = xmlDo c .Cr e at eEl eme nt ( "Boo kPar t i c ipa nt " ) ; xmlPar t i c ipa nt T ype = xmlDo c .Cr e at eAt t r ibut e ( " t ype " ) ; xmlPar t i c ipa nt T ype. In ner T e x t = " E d i t o r " ; xmlBo o kPar t i c ipa nt .At t r ibute s .Append ( xmlPar t i c ipa nt T ype ) ; xml F i r s tName = xmlDo c .Cr e at eE l eme nt ( " F i r s tName " ) ; xml F i r s tName. Inn er T e x t = " Ewa n" ; xmlBo o kPar t i c ipa nt .Appe ndCh i ld ( xml F i r s tName ) ; xml L as tName = xmlDo c .Cr e at eE l eme nt ( " Las tName " ) ; xml L as tName. In ner T e x t = "Buc ki ngh am" ; xmlBoo kPar t i c ipa nt .Appe ndCh i ld ( xml La s tName ) ; xmlBo o kPar t i c ipa nt s .Appe ndCh i ld ( xmlBo o kPar t i c ipa nt ) ;
  • 16. LINQ TO XML – BUILDING AN ELEMENT var xBookParticipants = new XElement("BookParticipants", new XElement("BookParticipant", new XAttribute("type", "Author"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")), new XElement("BookParticipant", new XAttribute("type", "Editor"), new XElement("FirstName", "Ewan"), new XElement("LastName", "Buckingham")));
  • 17. LINQ TO XML - METHODS doc.Save(@"E:fi le.xml") ; doc.Save(@"E:fi le.xml",SaveOptions.DisableFormatt ing) ; XDocument xmlDoc1 = XDocument .Load("TestFi le.xml") ; XElement name = new XElement ("Author", "Hammod") ; Console.Wr i teLine(name.Value) ;
  • 18. LINQ TO XML - XELEMENT str ing[ ] names = { "M.Hammod", "Golden Man", "Mohammed_807", "Nawwar" } ; XElement OurGroupMembers = new XElement("OurGroup", from n in names select new XElement("Name",n) ) ; Console.Wr i teLine(OurGroupMembers);
  • 19. LINQ TO XML - XSTREAMING XStreamingElement OurGroupMembers = new XStreamingElement("OurGroup", from n in names select new XElement("Name", n)); names[3] = "Tamer"; Console.WriteLine(OurGroupMembers); names[2] = "Smer"; Console.WriteLine(OurGroupMembers); XStreamingElement VS. XElement
  • 20. LINQ TO XML - XSTREAMING <OurGroup> <Name>M.Hammod</Name> <Name>Golden Man</Name> <Name>Mohammed_807</Name> <Name>Tamer</Name> </OurGroup> <OurGroup> <Name>M.Hammod</Name> <Name>Golden Man</Name> <Name>Smer</Name> <Name>Tamer</Name> </OurGroup>
  • 21. LINQ TO XML – ADDING ELEMENTS xDocument1.Element("BookParticipants").Add( new XElement("BookParticipant", new XAttribute("type", "Editor"), new XElement("FirstName", "Mohammad"), new XElement("LastName", "_807"))); - AddFi rst . - AddBeforeSel f . - AddAf terSel f .
  • 22. LINQ TO XML – ADDING ELEMENTS xDocument1.Element("BookParticipants").Elements("BookParticipant"). Where(e => ((string)e.Element("FirstName")) =="Tarek"). Single<XElement>().AddBeforeSelf( new XElement("BookParticipant", new XAttribute("type", "Technical Reviewer"), new XElement("FirstName", "Nawwar"), new XElement("LastName", "soso")));
  • 23. XElement elementToUpdate = xDocument1.Element("BookParticipants"). Elements("BookParticipant"). Single(e=>((string)e.Element("FirstName")) == "Tarek"); //using Value to Update elementToUpdate.Element("FirstName").Value = "New_Tarek"; - ReplaceAll LINQ TO XML - UPDATING
  • 24. LINQ TO XML – REMOVE ELEMENTS - elementToRemove.Remove(); - xDocument1.Descendants( ) .Where(e => e.Name =="Fi rstName" ) .Remove( ) ; - xDocument1.Element ("BookPar t icipants" ) .RemoveAl l ( ) ;
  • 25. LINQ TO SQL Linq To Sql Demo
  • 26. LINQ – FUNC AND DELEGATES publ ic stat ic IEnumerable<TResul t> Select<TSource, TResul t>(this IEnumerable<TSource> source, Func<TSource, TResul t> selector ) ; publ ic delegate st r ing MyDelegate( int index) ; publ ic stat ic st r ing ReturnName( int index) { st r ing[ ] names3 = new st r ing[ ] { "Hammod", "Tarek", "Alyan", "Nawwar" } ; return names3[ index] ; } stat ic MyDelegate SDelegate = new MyDelegate(ReturnName) ; Console.Wr i teLine(SDelegate(1) ) ;
  • 27. LINQ – FUNC AND DELEGATES Mohammad AL- Hammod. m-khaled89@hotmai l .com Damascus – 2011. Deep Thanks