LIN{UX,Q} : It's time for a refreshment(2)

Setelah refreshment pertama, sekarang refreshment kedua, tentang salah satu fitur .Net yang pernah dibahas pada dotnet internal meeting INDC tahun 2005 lalu. Ya, LINQ sudah terdengar 3 tahun lalu, tapi sampai sekarang belum banyak dimanfaatkan, mungkin karena belum cukup matang dan juga belum didukung oleh versi Visual Studio 2005/.Net 2.0 secara penuh. Setelah Visual Studio 2008 dan .Net 3.5 diluncurkan, barulah kita bisa mulai serius menggunakan LINQ. Kini saatnya untuk menyegarkan kembali dan memperjelas apa yang pernah kita dengar samar-samar sekian tahun lalu.

Apa yang ingin dicapai dengan LINQ? membuat programming menjadi lebih mudah. Dengan LINQ, kita cukup menggunakan satu syntax untuk meng-query berbagai macam sumber data. Tidak peduli sumber datanya dari mana, dari sql-server, dari dataset, dari object, dari file xml atau dari sekumpulan apapun, kita bisa menggunakan syntax yang sama untuk menarik ataupun mengupdate data sesuai yang kita inginkan. Tidak perlu tahu T-SQL untuk query database, tidak perlu tahu Xpath untuk query xml dsb.

image

Bagaimana LINQ membuat programming lebih mudah? sambil lalu kita lihat juga beberapa fitur terbaru dari c# 3.0.

Berikut adalah contoh bagaimana query dari sebuah object yang berisi kumpulan object dilakukan dengan vs2005/c#2.0

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4:  
   5: class Customer
   6: {
   7:  
   8:     string customerID;
   9:     string contactName;
  10:     string city;
  11:  
  12:     public string CustomerID
  13:     {
  14:         get { return customerID; }
  15:         set { customerID = value; }
  16:     }
  17:  
  18:     public string ContactName
  19:     {
  20:         get { return contactName; }
  21:         set { contactName = value; }
  22:     }
  23:  
  24:     public string City
  25:     {
  26:         get { return city; }
  27:         set { city = value; }
  28:     }
  29: }
  30: class Driver
  31: {
  32:     static void Main(string[] args)
  33:     {
  34:         List<Result> results = new List<Result>();
  35:         foreach (Customer c in LoadCustomers())
  36:         {
  37:  
  38:             if (c.City.StartsWith("M"))
  39:             {
  40:                 Result res = results.Find(delegate(Result r)
  41:                      { return c.City == r.City; });
  42:  
  43:                 if (res == null)
  44:                 {
  45:  
  46:                     res = new Result();
  47:                     res.City = c.City;
  48:                     res.Count = 1;
  49:                     results.Add(res);
  50:                 }
  51:                 else
  52:                 {
  53:  
  54:                     res.Count++;
  55:                 }
  56:             }
  57:         }
  58:         foreach (Result r in results)
  59:         {
  60:             Console.WriteLine("City:{0}\tCount:{1}", r.City, r.Count);
  61:         }
  62:     }
  63:  
  64:     static IEnumerable<Customer> LoadCustomers()
  65:     {
  66:  
  67:         List<Customer> custs = new List<Customer>();
  68:  
  69:         Customer c = new Customer();
  70:         c.CustomerID = "ALFKI";
  71:         c.ContactName = "Maria Anders";
  72:         c.City = "Berlin";
  73:         custs.Add(c);
  74:  
  75:         #region many customers ...
  76:  
  77:         c = new Customer();
  78:         c.CustomerID = "ANATR";
  79:         c.ContactName = "Ana Trujillo";
  80:         c.City = "México D.F.";
  81:         custs.Add(c);
  82:  
  83:         c = new Customer();
  84:         c.CustomerID = "ANTON";
  85:         c.ContactName = "Antonio Moreno";
  86:         c.City = "México D.F.";
  87:         custs.Add(c);
  88:  
  89:         c = new Customer();
  90:         c.CustomerID = "AROUT";
  91:         c.ContactName = "Thomas Hardy";
  92:         c.City = "London";
  93:         custs.Add(c);
  94:  
  95:         c = new Customer();
  96:         c.CustomerID = "BERGS";
  97:         c.ContactName = "Christina Berglund";
  98:         c.City = "Luleå";
  99:         custs.Add(c);
 100:  
 101:         c = new Customer();
 102:         c.CustomerID = "BLAUS";
 103:         c.ContactName = "Hanna Moos";
 104:         c.City = "Mannheim";
 105:         custs.Add(c);
 106:  
 107:         c = new Customer();
 108:         c.CustomerID = "BLONP";
 109:         c.ContactName = "Frédérique Citeaux";
 110:         c.City = "Strasbourg";
 111:         custs.Add(c);
 112:  
 113:         c = new Customer();
 114:         c.CustomerID = "BOLID";
 115:         c.ContactName = "Martín Sommer";
 116:         c.City = "Madrid";
 117:         custs.Add(c);
 118:  
 119:         c = new Customer();
 120:         c.CustomerID = "BONAP";
 121:         c.ContactName = "Laurence Lebihan";
 122:         c.City = "Marseille";
 123:         custs.Add(c);
 124:  
 125:         c = new Customer();
 126:         c.CustomerID = "BOTTM";
 127:         c.ContactName = "Elizabeth Lincoln";
 128:         c.City = "Tsawassen";
 129:         custs.Add(c);
 130:  
 131:         c = new Customer();
 132:         c.CustomerID = "BSBEV";
 133:         c.ContactName = "Victoria Ashworth";
 134:         c.City = "London";
 135:         custs.Add(c);
 136:  
 137:         c = new Customer();
 138:         c.CustomerID = "CACTU";
 139:         c.ContactName = "Patricio Simpson";
 140:         c.City = "Buenos Aires";
 141:         custs.Add(c);
 142:  
 143:         c = new Customer();
 144:         c.CustomerID = "CENTC";
 145:         c.ContactName = "Francisco Chang";
 146:         c.City = "México D.F.";
 147:         custs.Add(c);
 148:  
 149:         c = new Customer();
 150:         c.CustomerID = "CHOPS";
 151:         c.ContactName = "Yang Wang";
 152:         c.City = "Bern";
 153:         custs.Add(c);
 154:  
 155:         c = new Customer();
 156:         c.CustomerID = "COMMI";
 157:         c.ContactName = "Pedro Afonso";
 158:         c.City = "Sao Paulo";
 159:         custs.Add(c);
 160:  
 161:         c = new Customer();
 162:         c.CustomerID = "CONSH";
 163:         c.ContactName = "Elizabeth Brown";
 164:         c.City = "London";
 165:         custs.Add(c);
 166:  
 167:         c = new Customer();
 168:         c.CustomerID = "DRACD";
 169:         c.ContactName = "Sven Ottlieb";
 170:         c.City = "Aachen";
 171:         custs.Add(c);
 172:  
 173:         c = new Customer();
 174:         c.CustomerID = "DUMON";
 175:         c.ContactName = "Janine Labrune";
 176:         c.City = "Nantes";
 177:         custs.Add(c);
 178:  
 179:         c = new Customer();
 180:         c.CustomerID = "EASTC";
 181:         c.ContactName = "Ann Devon";
 182:         c.City = "London";
 183:         custs.Add(c);
 184:  
 185:         c = new Customer();
 186:         c.CustomerID = "ERNSH";
 187:         c.ContactName = "Roland Mendel";
 188:         c.City = "Graz";
 189:         custs.Add(c);
 190:  
 191:         c = new Customer();
 192:         c.CustomerID = "FAMIA";
 193:         c.ContactName = "Aria Cruz";
 194:         c.City = "Sao Paulo";
 195:         custs.Add(c);
 196:  
 197:         c = new Customer();
 198:         c.CustomerID = "FISSA";
 199:         c.ContactName = "Diego Roel";
 200:         c.City = "Madrid";
 201:         custs.Add(c);
 202:  
 203:         c = new Customer();
 204:         c.CustomerID = "FOLIG";
 205:         c.ContactName = "Martine Rancé";
 206:         c.City = "Lille";
 207:         custs.Add(c);
 208:  
 209:         c = new Customer();
 210:         c.CustomerID = "FOLKO";
 211:         c.ContactName = "Maria Larsson";
 212:         c.City = "Bräcke";
 213:         custs.Add(c);
 214:  
 215:         c = new Customer();
 216:         c.CustomerID = "FRANK";
 217:         c.ContactName = "Peter Franken";
 218:         c.City = "München";
 219:         custs.Add(c);
 220:  
 221:         c = new Customer();
 222:         c.CustomerID = "FRANR";
 223:         c.ContactName = "Carine Schmitt";
 224:         c.City = "Nantes";
 225:         custs.Add(c);
 226:  
 227:         c = new Customer();
 228:         c.CustomerID = "FRANS";
 229:         c.ContactName = "Paolo Accorti";
 230:         c.City = "Torino";
 231:         custs.Add(c);
 232:  
 233:         c = new Customer();
 234:         c.CustomerID = "FURIB";
 235:         c.ContactName = "Lino Rodriguez";
 236:         c.City = "Lisboa";
 237:         custs.Add(c);
 238:  
 239:         c = new Customer();
 240:         c.CustomerID = "GALED";
 241:         c.ContactName = "Eduardo Saavedra";
 242:         c.City = "Barcelona";
 243:         custs.Add(c);
 244:  
 245:         c = new Customer();
 246:         c.CustomerID = "GODOS";
 247:         c.ContactName = "José Pedro Freyre";
 248:         c.City = "Sevilla";
 249:         custs.Add(c);
 250:  
 251:         c = new Customer();
 252:         c.CustomerID = "GOURL";
 253:         c.ContactName = "André Fonseca";
 254:         c.City = "Campinas";
 255:         custs.Add(c);
 256:  
 257:         c = new Customer();
 258:         c.CustomerID = "GREAL";
 259:         c.ContactName = "Howard Snyder";
 260:         c.City = "Eugene";
 261:         custs.Add(c);
 262:  
 263:         c = new Customer();
 264:         c.CustomerID = "GROSR";
 265:         c.ContactName = "Manuel Pereira";
 266:         c.City = "Caracas";
 267:         custs.Add(c);
 268:  
 269:         c = new Customer();
 270:         c.CustomerID = "HANAR";
 271:         c.ContactName = "Mario Pontes";
 272:         c.City = "Rio de Janeiro";
 273:         custs.Add(c);
 274:  
 275:         c = new Customer();
 276:         c.CustomerID = "HILAA";
 277:         c.ContactName = "Carlos Hernández";
 278:         c.City = "San Cristóbal";
 279:         custs.Add(c);
 280:  
 281:         c = new Customer();
 282:         c.CustomerID = "HUNGC";
 283:         c.ContactName = "Yoshi Latimer";
 284:         c.City = "Elgin";
 285:         custs.Add(c);
 286:  
 287:         c = new Customer();
 288:         c.CustomerID = "HUNGO";
 289:         c.ContactName = "Patricia McKenna";
 290:         c.City = "Cork";
 291:         custs.Add(c);
 292:  
 293:         c = new Customer();
 294:         c.CustomerID = "ISLAT";
 295:         c.ContactName = "Helen Bennett";
 296:         c.City = "Cowes";
 297:         custs.Add(c);
 298:  
 299:         c = new Customer();
 300:         c.CustomerID = "KOENE";
 301:         c.ContactName = "Philip Cramer";
 302:         c.City = "Brandenburg";
 303:         custs.Add(c);
 304:  
 305:         c = new Customer();
 306:         c.CustomerID = "LACOR";
 307:         c.ContactName = "Daniel Tonini";
 308:         c.City = "Versailles";
 309:         custs.Add(c);
 310:  
 311:         c = new Customer();
 312:         c.CustomerID = "LAMAI";
 313:         c.ContactName = "Annette Roulet";
 314:         c.City = "Toulouse";
 315:         custs.Add(c);
 316:  
 317:         c = new Customer();
 318:         c.CustomerID = "LAUGB";
 319:         c.ContactName = "Yoshi Tannamuri";
 320:         c.City = "Vancouver";
 321:         custs.Add(c);
 322:  
 323:         c = new Customer();
 324:         c.CustomerID = "LAZYK";
 325:         c.ContactName = "John Steel";
 326:         c.City = "Walla Walla";
 327:         custs.Add(c);
 328:  
 329:         c = new Customer();
 330:         c.CustomerID = "LEHMS";
 331:         c.ContactName = "Renate Messner";
 332:         c.City = "Frankfurt a.M.";
 333:         custs.Add(c);
 334:  
 335:         c = new Customer();
 336:         c.CustomerID = "LETSS";
 337:         c.ContactName = "Jaime Yorres";
 338:         c.City = "San Francisco";
 339:         custs.Add(c);
 340:  
 341:         c = new Customer();
 342:         c.CustomerID = "LILAS";
 343:         c.ContactName = "Carlos González";
 344:         c.City = "Barquisimeto";
 345:         custs.Add(c);
 346:  
 347:         c = new Customer();
 348:         c.CustomerID = "LINOD";
 349:         c.ContactName = "Felipe Izquierdo";
 350:         c.City = "I. de Margarita";
 351:         custs.Add(c);
 352:  
 353:         c = new Customer();
 354:         c.CustomerID = "LONEP";
 355:         c.ContactName = "Fran Wilson";
 356:         c.City = "Portland";
 357:         custs.Add(c);
 358:  
 359:         c = new Customer();
 360:         c.CustomerID = "MAGAA";
 361:         c.ContactName = "Giovanni Rovelli";
 362:         c.City = "Bergamo";
 363:         custs.Add(c);
 364:  
 365:         c = new Customer();
 366:         c.CustomerID = "MAISD";
 367:         c.ContactName = "Catherine Dewey";
 368:         c.City = "Bruxelles";
 369:         custs.Add(c);
 370:  
 371:         c = new Customer();
 372:         c.CustomerID = "MEREP";
 373:         c.ContactName = "Jean Fresnière";
 374:         c.City = "Montréal";
 375:         custs.Add(c);
 376:  
 377:         c = new Customer();
 378:         c.CustomerID = "MORGK";
 379:         c.ContactName = "Alexander Feuer";
 380:         c.City = "Leipzig";
 381:         custs.Add(c);
 382:  
 383:         c = new Customer();
 384:         c.CustomerID = "NORTS";
 385:         c.ContactName = "Simon Crowther";
 386:         c.City = "London";
 387:         custs.Add(c);
 388:  
 389:         c = new Customer();
 390:         c.CustomerID = "OCEAN";
 391:         c.ContactName = "Yvonne Moncada";
 392:         c.City = "Buenos Aires";
 393:         custs.Add(c);
 394:  
 395:         c = new Customer();
 396:         c.CustomerID = "OLDWO";
 397:         c.ContactName = "Rene Phillips";
 398:         c.City = "Anchorage";
 399:         custs.Add(c);
 400:  
 401:         c = new Customer();
 402:         c.CustomerID = "OTTIK";
 403:         c.ContactName = "Henriette Pfalzheim";
 404:         c.City = "Köln";
 405:         custs.Add(c);
 406:  
 407:         c = new Customer();
 408:         c.CustomerID = "PARIS";
 409:         c.ContactName = "Marie Bertrand";
 410:         c.City = "Paris";
 411:         custs.Add(c);
 412:  
 413:         c = new Customer();
 414:         c.CustomerID = "PERIC";
 415:         c.ContactName = "Guillermo Fernández";
 416:         c.City = "México D.F.";
 417:         custs.Add(c);
 418:  
 419:         c = new Customer();
 420:         c.CustomerID = "PICCO";
 421:         c.ContactName = "Georg Pipps";
 422:         c.City = "Salzburg";
 423:         custs.Add(c);
 424:  
 425:         c = new Customer();
 426:         c.CustomerID = "PRINI";
 427:         c.ContactName = "Isabel de Castro";
 428:         c.City = "Lisboa";
 429:         custs.Add(c);
 430:  
 431:         c = new Customer();
 432:         c.CustomerID = "QUEDE";
 433:         c.ContactName = "Bernardo Batista";
 434:         c.City = "Rio de Janeiro";
 435:         custs.Add(c);
 436:  
 437:         c = new Customer();
 438:         c.CustomerID = "QUEEN";
 439:         c.ContactName = "Lúcia Carvalho";
 440:         c.City = "Sao Paulo";
 441:         custs.Add(c);
 442:  
 443:         c = new Customer();
 444:         c.CustomerID = "QUICK";
 445:         c.ContactName = "Horst Kloss";
 446:         c.City = "Cunewalde";
 447:         custs.Add(c);
 448:  
 449:         c = new Customer();
 450:         c.CustomerID = "RANCH";
 451:         c.ContactName = "Sergio Gutiérrez";
 452:         c.City = "Buenos Aires";
 453:         custs.Add(c);
 454:  
 455:         c = new Customer();
 456:         c.CustomerID = "RATTC";
 457:         c.ContactName = "Paula Wilson";
 458:         c.City = "Albuquerque";
 459:         custs.Add(c);
 460:  
 461:         c = new Customer();
 462:         c.CustomerID = "REGGC";
 463:         c.ContactName = "Maurizio Moroni";
 464:         c.City = "Reggio Emilia";
 465:         custs.Add(c);
 466:  
 467:         c = new Customer();
 468:         c.CustomerID = "RICAR";
 469:         c.ContactName = "Janete Limeira";
 470:         c.City = "Rio de Janeiro";
 471:         custs.Add(c);
 472:  
 473:         c = new Customer();
 474:         c.CustomerID = "RICSU";
 475:         c.ContactName = "Michael Holz";
 476:         c.City = "Genève";
 477:         custs.Add(c);
 478:  
 479:         c = new Customer();
 480:         c.CustomerID = "ROMEY";
 481:         c.ContactName = "Alejandra Camino";
 482:         c.City = "Madrid";
 483:         custs.Add(c);
 484:  
 485:         c = new Customer();
 486:         c.CustomerID = "SANTG";
 487:         c.ContactName = "Jonas Bergulfsen";
 488:         c.City = "Stavern";
 489:         custs.Add(c);
 490:  
 491:         c = new Customer();
 492:         c.CustomerID = "SAVEA";
 493:         c.ContactName = "Jose Pavarotti";
 494:         c.City = "Boise";
 495:         custs.Add(c);
 496:  
 497:         c = new Customer();
 498:         c.CustomerID = "SEVES";
 499:         c.ContactName = "Hari Kumar";
 500:         c.City = "London";
 501:         custs.Add(c);
 502:  
 503:         c = new Customer();
 504:         c.CustomerID = "SIMOB";
 505:         c.ContactName = "Jytte Petersen";
 506:         c.City = "Kobenhavn";
 507:         custs.Add(c);
 508:  
 509:         c = new Customer();
 510:         c.CustomerID = "SPECD";
 511:         c.ContactName = "Dominique Perrier";
 512:         c.City = "Paris";
 513:         custs.Add(c);
 514:  
 515:         c = new Customer();
 516:         c.CustomerID = "SPLIR";
 517:         c.ContactName = "Art Braunschweiger";
 518:         c.City = "Lander";
 519:         custs.Add(c);
 520:  
 521:         c = new Customer();
 522:         c.CustomerID = "SUPRD";
 523:         c.ContactName = "Pascale Cartrain";
 524:         c.City = "Charleroi";
 525:         custs.Add(c);
 526:  
 527:         c = new Customer();
 528:         c.CustomerID = "THEBI";
 529:         c.ContactName = "Liz Nixon";
 530:         c.City = "Portland";
 531:         custs.Add(c);
 532:  
 533:         c = new Customer();
 534:         c.CustomerID = "THECR";
 535:         c.ContactName = "Liu Wong";
 536:         c.City = "Butte";
 537:         custs.Add(c);
 538:  
 539:         c = new Customer();
 540:         c.CustomerID = "TOMSP";
 541:         c.ContactName = "Karin Josephs";
 542:         c.City = "Münster";
 543:         custs.Add(c);
 544:  
 545:         c = new Customer();
 546:         c.CustomerID = "TORTU";
 547:         c.ContactName = "Miguel Angel Paolino";
 548:         c.City = "México D.F.";
 549:         custs.Add(c);
 550:  
 551:         c = new Customer();
 552:         c.CustomerID = "TRADH";
 553:         c.ContactName = "Anabela Domingues";
 554:         c.City = "Sao Paulo";
 555:         custs.Add(c);
 556:  
 557:         c = new Customer();
 558:         c.CustomerID = "TRAIH";
 559:         c.ContactName = "Helvetius Nagy";
 560:         c.City = "Kirkland";
 561:         custs.Add(c);
 562:  
 563:         c = new Customer();
 564:         c.CustomerID = "VAFFE";
 565:         c.ContactName = "Palle Ibsen";
 566:         c.City = "Århus";
 567:         custs.Add(c);
 568:  
 569:         c = new Customer();
 570:         c.CustomerID = "VICTE";
 571:         c.ContactName = "Mary Saveley";
 572:         c.City = "Lyon";
 573:         custs.Add(c);
 574:  
 575:         c = new Customer();
 576:         c.CustomerID = "VINET";
 577:         c.ContactName = "Paul Henriot";
 578:         c.City = "Reims";
 579:         custs.Add(c);
 580:  
 581:         c = new Customer();
 582:         c.CustomerID = "WANDK";
 583:         c.ContactName = "Rita Müller";
 584:         c.City = "Stuttgart";
 585:         custs.Add(c);
 586:  
 587:         c = new Customer();
 588:         c.CustomerID = "WARTH";
 589:         c.ContactName = "Pirkko Koskitalo";
 590:         c.City = "Oulu";
 591:         custs.Add(c);
 592:  
 593:         c = new Customer();
 594:         c.CustomerID = "WELLI";
 595:         c.ContactName = "Paula Parente";
 596:         c.City = "Resende";
 597:         custs.Add(c);
 598:  
 599:         c = new Customer();
 600:         c.CustomerID = "WHITC";
 601:         c.ContactName = "Karl Jablonski";
 602:         c.City = "Seattle";
 603:         custs.Add(c);
 604:  
 605:         c = new Customer();
 606:         c.CustomerID = "WILMK";
 607:         c.ContactName = "Matti Karttunen";
 608:         c.City = "Helsinki";
 609:         custs.Add(c);
 610:         #endregion
 611:  
 612:         c = new Customer();
 613:         c.CustomerID = "WOLZA";
 614:         c.ContactName = "Zbyszek Piestrzeniewicz";
 615:         c.City = "Warszawa";
 616:         custs.Add(c);
 617:  
 618:         return custs;
 619:     }
 620: }
 621: class Result
 622: {
 623:  
 624:     private string city;
 625:  
 626:     private int count;
 627:  
 628:     public string City
 629:     {
 630:         get { return city; }
 631:         set { city = value; }
 632:     }
 633:  
 634:     public int Count
 635:     {
 636:         get { return count; }
 637:         set { count = value; }
 638:     }
 639: }

ketika code ini dijalankan berikut adalah hasilnya:

linq2

Perhatikan bagaimana query dilakukan untuk menghitung berapa customer dari setiap kota yang diawali dengan hurum "M".

   1: static void Main(string[] args)
   2: {
   3:     List<Result> results = new List<Result>();
   4:     foreach (Customer c in LoadCustomers())
   5:     {
   6:  
   7:         if (c.City.StartsWith("M"))
   8:         {
   9:             Result res = results.Find(delegate(Result r)
  10:                  { return c.City == r.City; });
  11:  
  12:             if (res == null)
  13:             {
  14:  
  15:                 res = new Result();
  16:                 res.City = c.City;
  17:                 res.Count = 1;
  18:                 results.Add(res);
  19:             }
  20:             else
  21:             {
  22:  
  23:                 res.Count++;
  24:             }
  25:         }
  26:     }
  27:     foreach (Result r in results)
  28:     {
  29:         Console.WriteLine("City:{0}\tCount:{1}", r.City, r.Count);
  30:     }
  31:     Console.ReadKey();
  32: }

 

Bagaimana vs2008/c#3.0 mempermudah kita melakukan hal yang sama? berikut kode c#3.0 untuk melakukan hal yang sama.

   1: static void Main(string[] args)
   2: {
   3:     var results = from c in LoadCustomers()
   4:                 where c.City.StartsWith("M")
   5:                 group c by c.City into g
   6:                 select new Result { City = g.Key, Count = g.Count() };
   7:     foreach (var r in results)
   8:     {
   9:         Console.WriteLine("City:{0}\tCount:{1}", r.City, r.Count);
  10:     }
  11:     Console.ReadKey();
  12: }

 

C#3.0 dengan LINQ akan sangat membantu kita dalam mengquery data dan selain itu juga dalam hal jumlah baris code yang harus ditulis.

Sebagai bonus, coba kita bandingkan cara lama dan cara baru untuk menset properties dari class:

di visual studio bisa kita lakukan dengan mengetik di awal baris:

prop tab tab

hasilnya kita bandingkan:

pada vs2005/C# 2.0:

   1: class Customer
   2: {
   3:  
   4:     string customerID;
   5:     string contactName;
   6:     string city;
   7:  
   8:     public string CustomerID
   9:     {
  10:         get { return customerID; }
  11:         set { customerID = value; }
  12:     }
  13:  
  14:     public string ContactName
  15:     {
  16:         get { return contactName; }
  17:         set { contactName = value; }
  18:     }
  19:  
  20:     public string City
  21:     {
  22:         get { return city; }
  23:         set { city = value; }
  24:     }
  25: }

 

pada vs2008/C# 3.0 jadinya:

   1: class Customer
   2: {
   3:     public string CustomerID { get; set; }
   4:     public string ContactName { get; set; }
   5:     public string City { get; set; }
   6: }

Jauh lebih singkat dan bersih.

Berikut adalah listing code yang lengkap dengan menggunakan fitur C# 3.0

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Transactions;
   5: using System.Data.Linq;
   6: using System.Xml.Linq;
   7:  
   8:  
   9: class Customer
  10: {
  11:     public string CustomerID { get; set; }
  12:     public string ContactName { get; set; }
  13:     public string City { get; set; }
  14: }
  15:  
  16: class Driver
  17: {
  18:     static void Main(string[] args)
  19:     {
  20:         var results = from c in LoadCustomers()
  21:                     where c.City.StartsWith("M")
  22:                     group c by c.City into g
  23:                     select new Result { City = g.Key, Count = g.Count() };
  24:         
  25:         foreach (var r in results)
  26:         {
  27:             Console.WriteLine("City:{0}\tCount:{1}", r.City, r.Count);
  28:         } 
  29:         Console.ReadKey();
  30:     }
  31:  
  32:  
  33:     static IEnumerable<Customer> LoadCustomers()
  34:     {
  35:             var custs = new List<Customer> {
  36:             new Customer { CustomerID = "ALFKI", ContactName = "Maria Anders", City = "Berlin"},
  37:             new Customer { CustomerID = "ANATR", ContactName = "Ana Trujillo", City = "México D.F."},
  38: #region more customers ...
  39:             new Customer { CustomerID = "ANTON", ContactName = "Antonio Moreno", City = "México D.F."},
  40:             new Customer { CustomerID = "AROUT", ContactName = "Thomas Hardy", City = "London"},
  41:             new Customer { CustomerID = "BERGS", ContactName = "Christina Berglund", City = "Luleå"},
  42:             new Customer { CustomerID = "BLAUS", ContactName = "Hanna Moos", City = "Mannheim"},
  43:             new Customer { CustomerID = "BLONP", ContactName = "Frédérique Citeaux", City = "Strasbourg"},
  44:             new Customer { CustomerID = "BOLID", ContactName = "Martín Sommer", City = "Madrid"},
  45:             new Customer { CustomerID = "BONAP", ContactName = "Laurence Lebihan", City = "Marseille"},
  46:             new Customer { CustomerID = "BOTTM", ContactName = "Elizabeth Lincoln", City = "Tsawassen"},
  47:             new Customer { CustomerID = "BSBEV", ContactName = "Victoria Ashworth", City = "London"},
  48:             new Customer { CustomerID = "CACTU", ContactName = "Patricio Simpson", City = "Buenos Aires"},
  49:             new Customer { CustomerID = "CENTC", ContactName = "Francisco Chang", City = "México D.F."},
  50:             new Customer { CustomerID = "CHOPS", ContactName = "Yang Wang", City = "Bern"},
  51:             new Customer { CustomerID = "COMMI", ContactName = "Pedro Afonso", City = "Sao Paulo"},
  52:             new Customer { CustomerID = "CONSH", ContactName = "Elizabeth Brown", City = "London"},
  53:             new Customer { CustomerID = "DRACD", ContactName = "Sven Ottlieb", City = "Aachen"},
  54:             new Customer { CustomerID = "DUMON", ContactName = "Janine Labrune", City = "Nantes"},
  55:             new Customer { CustomerID = "EASTC", ContactName = "Ann Devon", City = "London"},
  56:             new Customer { CustomerID = "ERNSH", ContactName = "Roland Mendel", City = "Graz"},
  57:             new Customer { CustomerID = "FAMIA", ContactName = "Aria Cruz", City = "Sao Paulo"},
  58:             new Customer { CustomerID = "FISSA", ContactName = "Diego Roel", City = "Madrid"},
  59:             new Customer { CustomerID = "FOLIG", ContactName = "Martine Rancé", City = "Lille"},
  60:             new Customer { CustomerID = "FOLKO", ContactName = "Maria Larsson", City = "Bräcke"},
  61:             new Customer { CustomerID = "FRANK", ContactName = "Peter Franken", City = "München"},
  62:             new Customer { CustomerID = "FRANR", ContactName = "Carine Schmitt", City = "Nantes"},
  63:             new Customer { CustomerID = "FRANS", ContactName = "Paolo Accorti", City = "Torino"},
  64:             new Customer { CustomerID = "FURIB", ContactName = "Lino Rodriguez", City = "Lisboa"},
  65:             new Customer { CustomerID = "GALED", ContactName = "Eduardo Saavedra", City = "Barcelona"},
  66:             new Customer { CustomerID = "GODOS", ContactName = "José Pedro Freyre", City = "Sevilla"},
  67:             new Customer { CustomerID = "GOURL", ContactName = "André Fonseca", City = "Campinas"},
  68:             new Customer { CustomerID = "GREAL", ContactName = "Howard Snyder", City = "Eugene"},
  69:             new Customer { CustomerID = "GROSR", ContactName = "Manuel Pereira", City = "Caracas"},
  70:             new Customer { CustomerID = "HANAR", ContactName = "Mario Pontes", City = "Rio de Janeiro"},
  71:             new Customer { CustomerID = "HILAA", ContactName = "Carlos Hernández", City = "San Cristóbal"},
  72:             new Customer { CustomerID = "HUNGC", ContactName = "Yoshi Latimer", City = "Elgin"},
  73:             new Customer { CustomerID = "HUNGO", ContactName = "Patricia McKenna", City = "Cork"},
  74:             new Customer { CustomerID = "ISLAT", ContactName = "Helen Bennett", City = "Cowes"},
  75:             new Customer { CustomerID = "KOENE", ContactName = "Philip Cramer", City = "Brandenburg"},
  76:             new Customer { CustomerID = "LACOR", ContactName = "Daniel Tonini", City = "Versailles"},
  77:             new Customer { CustomerID = "LAMAI", ContactName = "Annette Roulet", City = "Toulouse"},
  78:             new Customer { CustomerID = "LAUGB", ContactName = "Yoshi Tannamuri", City = "Vancouver"},
  79:             new Customer { CustomerID = "LAZYK", ContactName = "John Steel", City = "Walla Walla"},
  80:             new Customer { CustomerID = "LEHMS", ContactName = "Renate Messner", City = "Frankfurt a.M."},
  81:             new Customer { CustomerID = "LETSS", ContactName = "Jaime Yorres", City = "San Francisco"},
  82:             new Customer { CustomerID = "LILAS", ContactName = "Carlos González", City = "Barquisimeto"},
  83:             new Customer { CustomerID = "LINOD", ContactName = "Felipe Izquierdo", City = "I. de Margarita"},
  84:             new Customer { CustomerID = "LONEP", ContactName = "Fran Wilson", City = "Portland"},
  85:             new Customer { CustomerID = "MAGAA", ContactName = "Giovanni Rovelli", City = "Bergamo"},
  86:             new Customer { CustomerID = "MAISD", ContactName = "Catherine Dewey", City = "Bruxelles"},
  87:             new Customer { CustomerID = "MEREP", ContactName = "Jean Fresnière", City = "Montréal"},
  88:             new Customer { CustomerID = "MORGK", ContactName = "Alexander Feuer", City = "Leipzig"},
  89:             new Customer { CustomerID = "NORTS", ContactName = "Simon Crowther", City = "London"},
  90:             new Customer { CustomerID = "OCEAN", ContactName = "Yvonne Moncada", City = "Buenos Aires"},
  91:             new Customer { CustomerID = "OLDWO", ContactName = "Rene Phillips", City = "Anchorage"},
  92:             new Customer { CustomerID = "OTTIK", ContactName = "Henriette Pfalzheim", City = "Köln"},
  93:             new Customer { CustomerID = "PARIS", ContactName = "Marie Bertrand", City = "Paris"},
  94:             new Customer { CustomerID = "PERIC", ContactName = "Guillermo Fernández", City = "México D.F."},
  95:             new Customer { CustomerID = "PICCO", ContactName = "Georg Pipps", City = "Salzburg"},
  96:             new Customer { CustomerID = "PRINI", ContactName = "Isabel de Castro", City = "Lisboa"},
  97:             new Customer { CustomerID = "QUEDE", ContactName = "Bernardo Batista", City = "Rio de Janeiro"},
  98:             new Customer { CustomerID = "QUEEN", ContactName = "Lúcia Carvalho", City = "Sao Paulo"},
  99:             new Customer { CustomerID = "QUICK", ContactName = "Horst Kloss", City = "Cunewalde"},
 100:             new Customer { CustomerID = "RANCH", ContactName = "Sergio Gutiérrez", City = "Buenos Aires"},
 101:             new Customer { CustomerID = "RATTC", ContactName = "Paula Wilson", City = "Albuquerque"},
 102:             new Customer { CustomerID = "REGGC", ContactName = "Maurizio Moroni", City = "Reggio Emilia"},
 103:             new Customer { CustomerID = "RICAR", ContactName = "Janete Limeira", City = "Rio de Janeiro"},
 104:             new Customer { CustomerID = "RICSU", ContactName = "Michael Holz", City = "Genève"},
 105:             new Customer { CustomerID = "ROMEY", ContactName = "Alejandra Camino", City = "Madrid"},
 106:             new Customer { CustomerID = "SANTG", ContactName = "Jonas Bergulfsen", City = "Stavern"},
 107:             new Customer { CustomerID = "SAVEA", ContactName = "Jose Pavarotti", City = "Boise"},
 108:             new Customer { CustomerID = "SEVES", ContactName = "Hari Kumar", City = "London"},
 109:             new Customer { CustomerID = "SIMOB", ContactName = "Jytte Petersen", City = "Kobenhavn"},
 110:             new Customer { CustomerID = "SPECD", ContactName = "Dominique Perrier", City = "Paris"},
 111:             new Customer { CustomerID = "SPLIR", ContactName = "Art Braunschweiger", City = "Lander"},
 112:             new Customer { CustomerID = "SUPRD", ContactName = "Pascale Cartrain", City = "Charleroi"},
 113:             new Customer { CustomerID = "THEBI", ContactName = "Liz Nixon", City = "Portland"},
 114:             new Customer { CustomerID = "THECR", ContactName = "Liu Wong", City = "Butte"},
 115:             new Customer { CustomerID = "TOMSP", ContactName = "Karin Josephs", City = "Münster"},
 116:             new Customer { CustomerID = "TORTU", ContactName = "Miguel Angel Paolino", City = "México D.F."},
 117:             new Customer { CustomerID = "TRADH", ContactName = "Anabela Domingues", City = "Sao Paulo"},
 118:             new Customer { CustomerID = "TRAIH", ContactName = "Helvetius Nagy", City = "Kirkland"},
 119:             new Customer { CustomerID = "VAFFE", ContactName = "Palle Ibsen", City = "Århus"},
 120:             new Customer { CustomerID = "VICTE", ContactName = "Mary Saveley", City = "Lyon"},
 121:             new Customer { CustomerID = "VINET", ContactName = "Paul Henriot", City = "Reims"},
 122:             new Customer { CustomerID = "WANDK", ContactName = "Rita Müller", City = "Stuttgart"},
 123:             new Customer { CustomerID = "WARTH", ContactName = "Pirkko Koskitalo", City = "Oulu"},
 124:             new Customer { CustomerID = "WELLI", ContactName = "Paula Parente", City = "Resende"},
 125:             new Customer { CustomerID = "WHITC", ContactName = "Karl Jablonski", City = "Seattle"},
 126:             new Customer { CustomerID = "WILMK", ContactName = "Matti Karttunen", City = "Helsinki"},
 127: #endregion
 128:             new Customer { CustomerID = "WOLZA", ContactName = "Zbyszek Piestrzeniewicz", City = "Warszawa"}
 129:         };
 130:         return custs;
 131:     }
 132: }
 133: class Result
 134: {
 135:     public string City { get; set; }
 136:     public int Count { get; set; }
 137: }

Masih banyak lagi kelebihan2 dan teknik2 baru yang bisa dipelajari. silahkan login, dan download VS2008TrainingKit dari seksi download di situs geeks ini.

Selamat belajar

Share this post: | | | |
Published Tuesday, February 12, 2008 1:36 AM by tahir
Filed under:

Comments

# re: LIN{UX,Q} : It's time for a refreshment(2)

Thursday, February 14, 2008 10:59 AM by cahnom

Thanks...

C# 3.0 memang keren, lebih singkat, bersih. Kode kita tidak lagi writeonly alias susah dibaca.

# LIN{UX,Q} : It's time for a refreshment(3)

Sunday, February 17, 2008 12:13 PM by Tahir's blog

Setelah membandingkan bagaimana meng-query sekumpulan object dari sebuah object pada artikel sebelumnya

Powered by Community Server (Commercial Edition), by Telligent Systems