27
L anguage IN tegrated Q ue ry Before “Hello LINQ , ”! There is “Why LINQ ”!? WHY LINQ ?!

Linq introduction

Embed Size (px)

Citation preview

Page 1: Linq introduction

Language INtegrated Query

Before “Hello LINQ , ”!

There is “Why LINQ ”!?

WHYLINQ

?!

Page 2: Linq introduction

HELLO

LINQ!

Page 3: Linq introduction

Var Keyword

Extension Methods

Lambda Expression

LINQBASIC

S

Page 4: Linq introduction

var text = "Hello LINQ!“ ;

System.Collections.Generic.IEnumerable<bool>

class Full_of_Errors { var s = 9; public var MyMethod() { } public void MyMethod2(var t) { } }

VAR

KEYWORD

Page 5: Linq introduction

public static class StringExtensions { public static int ConvertToInt(this string number)

{ return Int32.Parse(number); }

public static string HammooD(this fl oat number){

return number.ToString(); } }

EXTENSIONMETHODS

Page 6: Linq introduction
Page 7: Linq introduction

string[] names = new string[] { "Hammod", "Tarek", "Alyan", "Nawwar" } ;

var name = names.Select(s => s.StartsWith("A"));

LAMBDA EXPRESSION

Page 8: Linq introduction

int[] fi bNum = { 1, 1, 2, 3, 5, 8, 13, 21, 34 };

double averageValue = fi bNum.Where(num => num % 2 == 1).Average();

SIMPLE EXAMPLES

Page 9: Linq introduction

var result = from b in Books where b.Price > 50.00

select b;

foreach (Book book in result){ Console.WriteLine(book.name)

}

SIMPLE EXAMPLES

Page 10: Linq introduction
Page 11: Linq introduction

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

Page 12: Linq introduction

SIMPLE EXAMPLES

First()FirstOrDefault()Single()SingleOrDefault()All()Any()Sum()Contains()……

Page 13: Linq introduction

SIMPLE EXAMPLES

int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

var numQuery = (from num in numbers where (num % 9) == 0 select num); if (numQuery.Any())

{ foreach (int num in numQuery)

{ Console.Write("{0} ", num);

}}

//more ideas?

Page 14: Linq introduction

<?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 – SIMPLE FILE

Page 15: Linq introduction

Xm lE l em ent x m l BookPa r t i c i pan t; Xm lAt t r i bu te x m lPa r t i c i pan tTy pe;

Xm lE l em ent x m l Fi r s tNam e; Xm lE l em ent x m l Las tName;

Xm lDocum en t x m l Doc = new Xm lDocum ent; ) ( Xm lE l em ent x m l BookPa r t i c i pan t s = x m lDoc .C rea teE lement ( " BookPa r t i c i pan t s " );

x m lDoc .Appen dCh i l d (x m lBookPa r t i c i pan t s ); x m lBookPar t i c i pan t = x m lDoc .C rea teE l em ent ( " BookPar t i c i pan t " );

x m lPa r t i c i pan tTy pe = x m lDoc .C rea teAt t r i bu te ( " t y pe" ); x m lPa r t i c i pan tTy pe . In nerTex t = " Au tho r; "

x m lBookPar t i c i pan t .A t t r i bu tes .Append (x m lPa r t i c i pan tTy pe); x m lFi r s tName = x m l Doc .C rea teE lem en t ( " Fi r s tName" );

x m lFi r s tName. Inne rTex t = " J oe; " x m lBookPar t i c i pan t .AppendCh i l d (x m l Fi r s tNam e);

x m lL as tNam e = x m lDoc .C rea teE lement ( " L as tName" ); x m lL as tNam e. Inn erTex t = " Ra t t z; "

x m lBookPar t i c i pan t .AppendCh i l d (x m l L as tName); x m lBookPar t i c i pan ts .Appen dCh i l d (x m lBookPa r t i c i pan t );

x m lBookPar t i c i pan t = x m lDoc .C rea teE l em ent ( " BookPar t i c i pan t " ); x m lPa r t i c i pan tTy pe = x m lDoc .C rea teAt t r i bu te ( " t y pe" );

x m lPa r t i c i pan tTy pe . In nerTex t = " Ed i to r; " x m lBookPar t i c i pan t .A t t r i bu tes .Append (x m lPa r t i c i pan tTy pe);

x m lFi r s tName = x m l Doc .C rea teE lem en t ( " Fi r s tName" ); x m lFi r s tName. Inne rTex t = "E wan; "

x m lBookPar t i c i pan t .AppendCh i l d (x m l Fi r s tNam e); x m lL as tNam e = x m lDoc .C rea teE lement ( " L as tName" );

x m lL as tNam e. Inn erTex t = " Buck ingham; " x m lBookPar t i c i pan t .AppendCh i l d (x m l L as tName);

x m lBookPar t i c i pan ts .Appen dCh i l d (x m lBookPa r t i c i pan t );

LINQ TO XML – DOM SYNTAX

Page 16: Linq introduction

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 – BUILDING AN ELEMENT

Page 17: Linq introduction

doc.Save(@"E:\fi le.xml");

doc.Save(@"E:\fi le.xml",SaveOptions.DisableFormatting);

XDocument xmlDoc1 = XDocument.Load("TestFile.xml");

XElement name = new XElement("Author", "Hammod"); Console.WriteLine(name.Value);

LINQ TO XML - METHODS

Page 18: Linq introduction

string[] names = { "M.Hammod", "Golden Man", "Mohammed_807", "Nawwar" };

XElement OurGroupMembers = new XElement("OurGroup", from n in names select new XElement("Name",n));

Console.WriteLine(OurGroupMembers);

LINQ TO XML - XELEMENT

Page 19: Linq introduction

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

Page 20: Linq introduction

<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 - XSTREAMING

Page 21: Linq introduction

xDocument1.Element("BookParticipants").Add( new XElement("BookParticipant", new XAttribute("type", "Editor"), new XElement("FirstName", "Mohammad"), new XElement("LastName", "_807")));

- AddFirst.

- AddBeforeSelf.

- AddAfterSelf.

LINQ TO XML – ADDING ELEMENTS

Page 22: Linq introduction

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

LINQ TO XML – ADDING ELEMENTS

Page 23: Linq introduction

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

Page 24: Linq introduction

- elementToRemove.Remove();

- xDocument1.Descendants().Where(e => e.Name =="FirstName").Remove();

- xDocument1.Element("BookParticipants").RemoveAll();

LINQ TO XML – REMOVE ELEMENTS

Page 25: Linq introduction

Linq To Sql Demo

LINQ TO SQL

Page 26: Linq introduction

public static IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, TResult> selector);

public delegate string MyDelegate(int index); public static string ReturnName(int index) { string[] names3 = new string[] { "Hammod", "Tarek", "Alyan", "Nawwar" }; return names3[index]; } static MyDelegate SDelegate = new MyDelegate(ReturnName);

Console.WriteLine(SDelegate(1));

LINQ – FUNC AND DELEGATES

Page 27: Linq introduction

Mohammad AL- [email protected] – 2011.

Deep Thanks

LINQ – FUNC AND DELEGATES