.
Anmeldung | Registrieren | Hilfe |
Suchen
Home Foren News Member Offers Termine Developer Blogs Knowledge Base

Navigation

Navigationslinks überspringen.
Knowledge Base reduzierenKnowledge Base
Tutorials reduzierenTutorials
Webentwicklung
Cliententwicklung
Datenbankentwicklung
IT Professional
Sharepoint
Sprachspezifisch reduzierenSprachspezifisch
C#
Visual Basic
C++
XAML
SQL
JavaScript
Erfahrungsberichte reduzierenErfahrungsberichte
Entwicklersoftware
Bücher
FAQ Grundlagen

Verknüpfungen

  • Knowledge Base durchsuchen
  • Hilfe zur Knowledge Base
  • RSS Feed
  • Twitter

.NET 4.0 - LINQ– Neue Extension Method: Zip

Ab .NET 4.0 folgt eine neue LINQ Extension Method mit „Zip“. Die Zip-Methode kombiniert zwei unterschiedliche Sequenzen (arrays, collections, listen etc.) zu einer Sequenz zusammen.

Hier ein kleines Beispiel:

   1:  int[] numbers = { 1, 2, 3, 4 };
   2:  string[] words = { "one", "two", "three" };
   3:   
   4:  var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
   5:  foreach (var item in numbersAndWords)
   6:    Console.WriteLine(item);
   7:   
   8:  // This code produces the following output:
   9:   
  10:  // 1 one
  11:  // 2 two
  12:  // 3 three

Dazu ist die Methode wie folgt Aufgebaut:

var kombinierteWerte = ersteWerte.Zip(zweiteWerte, (itemWert1, itemWert2)=>{ itemWert1 + itemWert2 });

Hier ein etwas umfangreicheres Beispiel:
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:   
   5:  namespace LinqWithZipExample
   6:  {
   7:      class Program
   8:      {
   9:          static void Main(string[] args)
  10:          {
  11:              // Arrange
  12:              List<Category> categories = new List<Category>();
  13:              Category category = new Category {ID = 1, Name = "Screen Designer"};
  14:              Category category2 = new Category {ID = 2, Name = "Developer"};
  15:              Category category3 = new Category {ID = 3, Name = "Solution Architect"};
  16:   
  17:              categories.Add(category);
  18:              categories.Add(category2);
  19:              categories.Add(category3);
  20:   
  21:              List<Employee> employees = new List<Employee>();
  22:              Employee employee = new Employee {Name = "Gregor Biswanger", Category = 3};
  23:              Employee employee2 = new Employee {Name = "Timo Winter", Category = 1};
  24:   
  25:              employees.Add(employee);
  26:              employees.Add(employee2);
  27:   
  28:              // Act
  29:              var result = employees.Zip(GetCategoryOfEmployee(employees, categories), (emp, cat) => emp.Name + " is " + cat.Name);
  30:              
  31:              // Output
  32:              foreach (string value in result)
  33:              {
  34:                  Console.WriteLine(value);
  35:              }
  36:   
  37:              Console.ReadLine();
  38:          }
  39:   
  40:          private static IEnumerable<Category> GetCategoryOfEmployee(IEnumerable<Employee> employees, IEnumerable<Category> categories)
  41:          {
  42:              return employees.SelectMany(employee => categories.Where(category => employee.Category == category.ID));
  43:          }
  44:      }
  45:   
  46:      internal class Category
  47:      {
  48:          public int ID { get; set; }
  49:          public string Name { get; set; }
  50:      }
  51:   
  52:      internal class Employee
  53:      {
  54:          public string Name { get; set; }
  55:          public int Category { get; set; }
  56:      }
  57:  }

Dabei kommt folgende Ausgabe:

linq-zip-example-output

Ein weiteres schönes Beispiel zeigt David Hayden, wie passend zu den Monaten die Anzahl der Monatstage mit Zip kombiniert und angezeigt werden können: http://www.davidhayden.me/2009/12/linq-zip---merge-two-sequences-using-predicate-function.html

Weitere Infos unter MSDN:
http://msdn.microsoft.com/en-us/library/dd267698.aspx

Viel Spaß und beste Grüße,
BFreakout

von BFreakout, 08.05.2010 zugeordnet zu C# .

Kommentare

Es sind noch keine Kommentare vorhanden.

Eigener Kommentar

Sie müssen angemeldet sein, um ein Kommentar zu erstellen.
  • Schwierigkeit: Einsteiger
  • Views: 1286
  • Zur Druckversion
  • Artikel von BFreakout

Kick it on dotnet-kicks.de

Artikel

Autor

Kick it!

Wenn ihnen dieser Artikel gefällt, bitte "kicken" sie ihn.

WPF Forum | ASP.NET Forum | ASP.NET MVC Forum | Silverlight Forum | Windows Phone 7 Forum | SharePoint Forum | Dotnet Jobs | Dotnet Termine | Developer Blogs | Dotnet News

Das Team | Regeln | Impressum