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") ) ;
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
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?
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 ( ) ;
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