using System;
using System.Linq ;
using System.Collections .Generic ;
namespace Articulos.Cap04
{
public sealed class ListGenericaExpresionLambda
{
public static void Main( )
{
List< string> paises = new List< string> { "Colombia" , "Perú" , "Canada" ,
"Francia" , "China" , "Groenlandia" , "Inglaterra" , "Haiti" , "Holanda"
} ;
// Espreción lambda pais => pais.Lenght <= 6 en método Where:
IEnumerable< string> consulta = paises.Where ( pais => pais.Length <= 6 ) ;
// Resultado:
Console.WriteLine ( "\n Nombres de países con número de caracteres <= 6:" ) ;
foreach ( string pais in consulta)
{
Console.WriteLine ( "\t {0}" , pais) ;
}
List< int> numeros = new List< int> { 0 , 1 , 2 , 3 , 5 , 6 , 7 , 8 , 9 , 10 } ;
// Genera `IEnumerable` con números impares:
IEnumerable< int> impares = numeros.Where ( numero => numero % 2 != 0 ) ;
// Genera `IEnumerable` con números pares:
IEnumerable< int> pares = numeros.Where ( numero => numero % 2 == 0 ) ;
// Resultado
Console.WriteLine ( "\n Números impares en `numeros`:" ) ;
foreach ( int numero in impares)
{
Console.WriteLine ( "\t {0}" , numero.ToString ( ) ) ;
}
// Resultado
Console.WriteLine ( "\n Números pares en `numeros`:" ) ;
foreach ( int numero in pares)
{
Console.WriteLine ( "\t {0}" , numero.ToString ( ) ) ;
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTGlucTsKdXNpbmcgU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWM7CgpuYW1lc3BhY2UgQXJ0aWN1bG9zLkNhcDA0CnsKICAgIHB1YmxpYyBzZWFsZWQgY2xhc3MgTGlzdEdlbmVyaWNhRXhwcmVzaW9uTGFtYmRhCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgTGlzdDxzdHJpbmc+IHBhaXNlcyA9IG5ldyBMaXN0PHN0cmluZz4geyAiQ29sb21iaWEiLCAiUGVyw7oiLCAiQ2FuYWRhIiwKICAgICAgICAgICAgICAgICJGcmFuY2lhIiwgIkNoaW5hIiwgIkdyb2VubGFuZGlhIiwgIkluZ2xhdGVycmEiLCAiSGFpdGkiLCAiSG9sYW5kYSIKICAgICAgICAgICAgfTsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIEVzcHJlY2nDs24gbGFtYmRhIHBhaXMgPT4gcGFpcy5MZW5naHQgPD0gNiBlbiBtw6l0b2RvIFdoZXJlOgogICAgICAgICAgICBJRW51bWVyYWJsZTxzdHJpbmc+IGNvbnN1bHRhID0gcGFpc2VzLldoZXJlKHBhaXMgPT4gcGFpcy5MZW5ndGggPD0gNik7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBSZXN1bHRhZG86CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCAiXG5Ob21icmVzIGRlIHBhw61zZXMgY29uIG7Dum1lcm8gZGUgY2FyYWN0ZXJlcyA8PSA2OiIpOwogICAgICAgICAgICBmb3JlYWNoIChzdHJpbmcgcGFpcyBpbiBjb25zdWx0YSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcdHswfSIsIHBhaXMpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBMaXN0PGludD4gbnVtZXJvcyA9IG5ldyBMaXN0PGludD4geyAwLCAxLCAyLCAzLCA1LCA2LCA3LCA4LCA5LCAxMH07CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBHZW5lcmEgYElFbnVtZXJhYmxlYCBjb24gbsO6bWVyb3MgaW1wYXJlczoKICAgICAgICAgICAgSUVudW1lcmFibGU8aW50PiBpbXBhcmVzID0gbnVtZXJvcy5XaGVyZSAobnVtZXJvID0+IG51bWVybyAlIDIgIT0gMCk7CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBHZW5lcmEgYElFbnVtZXJhYmxlYCBjb24gbsO6bWVyb3MgcGFyZXM6CiAgICAgICAgICAgIElFbnVtZXJhYmxlPGludD4gcGFyZXMgPSBudW1lcm9zLldoZXJlIChudW1lcm8gPT4gbnVtZXJvICUgMiA9PSAwKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIFJlc3VsdGFkbwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIlxuTsO6bWVyb3MgaW1wYXJlcyBlbiBgbnVtZXJvc2A6Iik7CiAgICAgICAgICAgIGZvcmVhY2ggICAgKGludCBudW1lcm8gaW4gaW1wYXJlcykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcdHswfSIsIG51bWVyby5Ub1N0cmluZygpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgLy8gUmVzdWx0YWRvCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXG5Ow7ptZXJvcyBwYXJlcyBlbiBgbnVtZXJvc2A6Iik7CiAgICAgICAgICAgIGZvcmVhY2ggICAgKGludCBudW1lcm8gaW4gcGFyZXMpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiXHR7MH0iLCBudW1lcm8uVG9TdHJpbmcoKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0=