Presentation is loading. Please wait.

Presentation is loading. Please wait.

Übersicht Einleitung zu LINQ LINQ Syntax LINQ to Objects (Übungen) LINQ to XML (Übungen) LINQ to SQL (Übungen) Fragen/Diskussion.

Similar presentations


Presentation on theme: "Übersicht Einleitung zu LINQ LINQ Syntax LINQ to Objects (Übungen) LINQ to XML (Übungen) LINQ to SQL (Übungen) Fragen/Diskussion."— Presentation transcript:

1

2 Übersicht Einleitung zu LINQ LINQ Syntax LINQ to Objects (Übungen) LINQ to XML (Übungen) LINQ to SQL (Übungen) Fragen/Diskussion

3 Was ist LINQ? Komponente zum Abfragen von Datenquellen Modifizieren und neu erstellen von Daten.Net 3.0 erste Version.Net 3.5 erweitert (anfragemanipulierende Erweiterungsmethoden).Net 4.0 PLINQ (Parallel LINQ) Standard Data Provider: LINQ to Objects, SQL, DataSets, XML

4 Vorteile und Nachteile VorteileNachteile Überprüfung durch CompilerTeilweise spürbar langsamer als Schleifen Gleicher Syntax für verschiedene Zwecke Neuer Syntax zum erlernen Code leserlicher und kürzerDebug wesentlich schwieriger PLINQ vereinfacht Multithreading

5 Basiswissen Lambda Expression x => x.Length > 5 (x, y) => x == y var keyword var myInteger = 1; var myString = Im a string.; Object Initialization Person adrian = new Person {Vorname = Adrian, Nachname = Weidermann}; var adrian = new Person {Vorname = Adrian, Nachname = Weidermann}; Anonymous Types var adrian = new {Vorname = Adrian, Nachname = Weidermann}; var tier = new {Gattung = Nashorn, Name = Kleiner Muck, Gewicht = 350, Alter = 2}; Extionsion Methods /// /// Extends all Enumerables. /// public static class EnumerableExtension { /// /// Executes the action for every element in list. /// /// The Target type. /// The enumerable source. /// The action to do. public static void ForEach (this Ienumerable source, Action action) { foreach (var item in source) { action(item); } } }

6 Sprachdetails Query Syntax An SQL angelehnt Übersichtlicher bei den meisten komplizierten Abfragen from x in persons where x.Vorname == Adrian Fluent Syntax Platzsparender Übersichtlicher bei den meisten einfachen Abfragen persons.Where(x => x.Vorname == Adrian)

7 Restriction Operators Where from customer in Customers where customer.Fax != null select customer Customers.Where((customer, index) => customer.Fax != null && index < 5)

8 Projection Operators Select from customer in Customers select new {Name = customer.ContactName.ToUpper(), Phone = customer.Phone} Customers.Select((customer, index) => new {Name = customer.ContactName.ToUpper(), Phone = customer.Phone, Index = index}) SelectMany from customer in Customers from order in customer.Orders select new {Name = customer.ContactName.ToUpper(), Phone = customer.Phone, order.OrderDate} Customers.SelectMany((customer, index) => customer.Orders.Select(order => Customer # + (index +1) + ordered on + order.OrderDate.ToString()))

9 Partitioning Operators Take (from customer in Customers select customer).Take(5) Customers.Take(5) Skip (from customer in Customers select customer).Skip(5) Customers.Skip(5) TakeWhile (from customer in Customers select customer).TakeWhile(customer => customer.Phone != null) Customers.TakeWhile(customer => customer.Phone != null) SkipWhile (from customer in Customers select customer).SkipWhile(customer => customer.Phone != null) Customers.SkipWhile(customer => customer.Phone != null)

10 Ordering Operators OrderBy from customer in Customers orderby customer.ContactName select customer Customers.OrderBy(customer => customer.ContactName) OrderByDescending from customer in Customers orderby customer.ContactName descending select customer Customers.OrderByDescending(customer => customer.ContactName) ThenBy from customer in Customers orderby customer.City descending, customer.ContactName select customer Customers.OrderBy(customer => customer.City).ThenBy(customer => customer.ContactName) ThenByDescending from customer in Customers orderby customer.City descending, customer.ContactName descending select customer Customers.OrderBy(customer => customer.City).ThenByDescending(customer => customer.ContactName) Reverse Customers.Reverse()

11 Grouping Operators GroupBy from customer in Customers group customer by customer.City into city select city Customers.GroupBy(customer => customer.City)

12 Set Operators Distinct (from customer in Customers select customer).Distinct() Customers.Distinct() Union (from customer in Customers select customer.ContactName).Union(from order in Orders select order.CustomerID) Customers.Select(customer => customer.ContactName).Union(Orders.Select(order => order.CustomerID)) Intersect (from customer in Customers select customer.ContactName).Intersect(from order in Orders select order.CustomerID) Customers.Select(customer => customer.ContactName).Intersect(Orders.Select(order => order.CustomerID)) Except (from customer in Customers select customer.ContactName).Except(from order in Orders select order.CustomerID) Customers.Select(customer => customer.ContactName).Except(Orders.Select(order => order.CustomerID))

13 Conversion Operators ToArray (from customer in Customers select customer).ToArray() Customers.ToArray() ToList (from customer in Customers select customer).ToList() Customers.ToList() ToDictionary (from customer in Customers select customer).ToDictionary(customer => customer.ContactName) Customers.ToDictionary(customer => customer.ContactName) OfType (from customer in Customers select customer.PostalCode).OfType () Customers.Select(customer => customer.PostalCode).OfType () Cast (from customer in Customers select customer.PostalCode).Cast () Customers.Select(customer => customer.PostalCode).Cast () ToLookup (from customer in Customers select customer).ToLookup(customer => customer.City = "Vienna") Customers.ToLookup(customer => customer.City = "Vienna") AsEnumerable (from customer in Customers select customer).AsEnumerable().Reverse() Customers.AsEnumerable().Reverse()

14 Element Operators First (from customer in Customers select customer).First(customer => customer.PostalCode == null) Customers.First(customer => customer.PostalCode == null) FirstOrDefault (from customer in Customers select customer).FirstOrDefault(customer => customer.PostalCode == null) Customers.FirstOrDefault(customer => customer.PostalCode == null) Single (from customer in Customers select customer).Single(customer => customer.PostalCode == null) Customers.Single(customer => customer.PostalCode == null) SingleOrDefault (from customer in Customers select customer).SingleOrDefault(customer => customer.PostalCode == null) Customers.SingleOrDefault(customer => customer.PostalCode == null) ElementAt (from customer in Customers select customer).ElementAt(1) Customers.ElementAt(1) ElementAtOrDefault (from customer in Customers select customer).ElementAtOrDefault(1) Customers.ElementAtOrDefault(1) DefaultIfEmpty (from customer in Customers select customer).DefaultIfEmpty().First() Customers.DefaultIfEmpty().First()

15 Generation Operators Range from n in Enumerable.Range(100, 50) select new { Number = n, OddEven = n % 2 == 1 ? "odd" : "even" } Repeat Enumerable.Repeat(RepeatMe, 10) Empty Enumerable.Empty ()

16 Quantifiers Any (from customer in Customers select customer).Any(customer => customer.Country != null) Customers.Any(customer => customer.Country != null) All (from customer in Customers select customer).All(customer => customer.Country != null) Customers.All(customer => customer.Country != null)

17 Aggregate Operators Count from customer in Customers group customer by customer.ContactTitle into customerGrouped select new { Customer = customerGrouped.Key, OrderCount = customerGrouped.Count() } Customers.Count() LongCount from customer in Customers group customer by customer.ContactTitle into customerGrouped select new { Customer = customerGrouped.Key, OrderCount = customerGrouped.LongCount() } Customers.LongCount() Sum (from order in Orders select order).Sum(order => order.Freight) Customers.Count() Min (from order in Orders select order).Min(order => order.Freight) Orders.Min(order => order.Freight) Max (from order in Orders select order).Max(order => order.Freight) Orders.Max(order => order.Freight) Average (from order in Orders select order).Average(order => order.Freight) Orders.Average(order => order.Freight) Aggregate (from order in Orders select order).Aggregate(100, (orderIDBalance, nextValue) => ((nextValue.OrderID <= orderIDBalance) ? (orderIDBalance - nextValue.OrderID) : orderIDBalance)) Orders.Select(order => order.OrderID).Aggregate((orderID, nextValue) => orderID * nextValue)

18 Miscellaneous Operators Concat (from customer in Customers select customer.ContactName).Concat(from order in Orders select order.ShipCity) Customers.Select(customer => customer.ContactName).Concat(Orders.Select(order => order.ShipCity)) EqualAll (from customer in Customers select customer.ContactName).SequenceEqual(from order in Orders select order.ShipCity) Customers.Select(customer => customer.ContactName).SequenceEqual(Orders.Select(order => order.ShipCity)) Let from customer in Customers let AddressInfo = customer.Address + " " + customer.PostalCode + " " + customer.City + " " + customer.Country select new { Name = customer.ContactName, Address = AddressInfo } Customers.Select(customer => new { Customer = customer, AddressInfo = customer.Address + " " + customer.PostalCode + " " + customer.City + " " + customer.Country }).Select(newCustomer => new { Name = newCustomer.Customer.ContactName, Address = newCustomer.AddressInfo })

19 JoinOperators Join from customer in Customers join order in Orders on customer.CustomerID equals order.CustomerID select new { Name = customer.ContactName, OrderDate = order.OrderDate} Customers.Join(Orders, customer => customer.CustomerID, order => order.CustomerID, (customer, order) => new { Name = customer.ContactName, OrderDate = order.OrderDate}) GroupJoin from customer in Customers join order in Orders on customer.CustomerID equals order.CustomerID into groupedOrder select new { Name = customer.ContactName, OrderDate = groupedOrder} Customers.GroupJoin(Orders, customer => customer.CustomerID, order => order.CustomerID, (customer, groupedOrder) => new { Name = customer.ContactName, OrderDate = groupedOrder}) LeftOuterJoin from customer in Customers join order in Orders on customer.CustomerID equals order.CustomerID into groupedOrder from order in groupedOrder select new { Name = customer.ContactName, OrderDate = groupedOrder}

20 LINQ to XML Erlaubt Lesen, schreiben von XML Dokumenten Vereinfacht lesen und schreiben von XML Daten

21 Object Model

22 Erklärung Object Models XDocument Ein XML Sheet (inkludiert laden und speichern) XDocument.Parse(String) erlaubt erstellen von Xelement aus string XDeclaration Informationen zum Sheet XDocumentType Definiert den Dokumenten Typ XText Alle String Objekte XCData Übernimmt den Text 1 : 1 also kein < zB. nötig Adrian ]]> XElement Child Element/Node im XML XElement.Parse(String) erlaubt erstellen von Xelement aus string

23 XAttribute Attribut eines Objektes XProcessingInstruction Informationen für Anwendungen XComment Kommentar XNamespace Namespace XStreamingElement Erlaubt einbetten von LINQ

24 Beispiel SEPA

25 Beispiel SEPA 2

26 Query Syntax Nodes Customers.Nodes() Customers.NodesBeforeSelf() Customers.NodesAfterSelf() Element(s) Customers.Elements() Customer.Element(ContactName) Customers.ElementsAfterSelf() Customers.ElementsBeforeSelf() Anchestor Orders.Anchestors() Orders.AnchestorsAndSelf() Descendants Customers.Descendants() Customers.DescendantsAndSelf() Attribute XElement.Attribute() XElement.Attributes() Annotation XObject.Annotation() XObject.Annotations()

27 Data Modification Add XContainer.Add(XElement) XContainer.AddFirst(XElement) XContainer.AddBeforeSelf(XElement) XContainer.AddAfterSelf(XElement) Remove XNode.Remove() IEnumerable.RemoveAll() ReplaceAll XElement.ReplaceAll(XElement, XElement)

28 Das wars… vorerst ;)


Download ppt "Übersicht Einleitung zu LINQ LINQ Syntax LINQ to Objects (Übungen) LINQ to XML (Übungen) LINQ to SQL (Übungen) Fragen/Diskussion."

Similar presentations


Ads by Google