// OrtizOL - xCSw - http://o...content-available-to-author-only...t.com
using System;
using System.Collections.Generic;
using System.Linq;
namespace Recetas.CSharp.R0525
{
internal class Fruta
{
public Fruta(string nombre, string color)
{
Nombre = nombre;
Color = color;
}
public string Nombre
{
get;
set;
}
public string Color
{
get;
set;
}
}
internal class FrutaLinq
{
public static void Main()
{
// Creación de la colección con objetos Fruta:
List<Fruta> frutas = new List<Fruta>(){
new Fruta("pera", "verde"),
new Fruta("mora", "rojo"),
new Fruta("cereza", "rojo"),
new Fruta("mango", "amarillo"),
new Fruta("banano", "amarillo"),
new Fruta("naranja", "naranaja"),
new Fruta("tomate", "rojo"),
new Fruta("melón", "amarillo"),
new Fruta("manzana", "verde")
};
// Seleccionamos los nombres de las frutas que no son rojas, y
// que su primera letra es distinta de `p`:
IEnumerable<string> resultado = from fruta in frutas where fruta.Color != "rojo"
&& fruta.Nombre[0] != 'p' orderby fruta.Nombre
select fruta.Nombre;
// Visualización de resultados:
Console.WriteLine("\nConsulta usando expresiones estándar:");
foreach(string nombre in resultado)
{
Console.WriteLine("\tResultado: {0}.", nombre);
}
// Ahora usamos expresiones lambda para la misma consulta:
resultado = frutas.Where(fruta => fruta.Color != "rojo" && fruta.Nombre[0] != 'p').
OrderBy(fruta => fruta.Nombre).Select(fruta => fruta.Nombre);
// Visualización de resultados:
Console.WriteLine("\nConsulta usando expresiones lambda:");
foreach(string nombre in resultado)
{
Console.WriteLine("\tResultado: {0}.", nombre);
}
Console.WriteLine();
}
}
}
Ly8gT3J0aXpPTCAtIHhDU3cgLSBodHRwOi8vby4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4udC5jb20KIAp1c2luZyBTeXN0ZW07CnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwp1c2luZyBTeXN0ZW0uTGlucTsKIApuYW1lc3BhY2UgUmVjZXRhcy5DU2hhcnAuUjA1MjUKewogICAgaW50ZXJuYWwgY2xhc3MgRnJ1dGEKICAgIHsKICAgICAgICBwdWJsaWMgRnJ1dGEoc3RyaW5nIG5vbWJyZSwgc3RyaW5nIGNvbG9yKQogICAgICAgIHsKICAgICAgICAgICAgTm9tYnJlID0gbm9tYnJlOwogICAgICAgICAgICBDb2xvciA9IGNvbG9yOwogICAgICAgIH0KICAgICAgICAKICAgICAgICBwdWJsaWMgc3RyaW5nIE5vbWJyZQogICAgICAgIHsKICAgICAgICAgICAgZ2V0OwogICAgICAgICAgICBzZXQ7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBzdHJpbmcgQ29sb3IKICAgICAgICB7CiAgICAgICAgICAgIGdldDsKICAgICAgICAgICAgc2V0OwogICAgICAgIH0KICAgIH0KICAgIAogICAgaW50ZXJuYWwgY2xhc3MgRnJ1dGFMaW5xCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgLy8gQ3JlYWNpw7NuIGRlIGxhIGNvbGVjY2nDs24gY29uIG9iamV0b3MgRnJ1dGE6CiAgICAgICAgICAgIExpc3Q8RnJ1dGE+IGZydXRhcyA9IG5ldyBMaXN0PEZydXRhPigpewogICAgICAgICAgICAgICAgbmV3IEZydXRhKCJwZXJhIiwgInZlcmRlIiksCiAgICAgICAgICAgICAgICBuZXcgRnJ1dGEoIm1vcmEiLCAicm9qbyIpLAogICAgICAgICAgICAgICAgbmV3IEZydXRhKCJjZXJlemEiLCAicm9qbyIpLAogICAgICAgICAgICAgICAgbmV3IEZydXRhKCJtYW5nbyIsICJhbWFyaWxsbyIpLAogICAgICAgICAgICAgICAgbmV3IEZydXRhKCJiYW5hbm8iLCAiYW1hcmlsbG8iKSwKICAgICAgICAgICAgICAgIG5ldyBGcnV0YSgibmFyYW5qYSIsICJuYXJhbmFqYSIpLAogICAgICAgICAgICAgICAgbmV3IEZydXRhKCJ0b21hdGUiLCAicm9qbyIpLAogICAgICAgICAgICAgICAgbmV3IEZydXRhKCJtZWzDs24iLCAiYW1hcmlsbG8iKSwKICAgICAgICAgICAgICAgIG5ldyBGcnV0YSgibWFuemFuYSIsICJ2ZXJkZSIpCiAgICAgICAgICAgIH07CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBTZWxlY2Npb25hbW9zIGxvcyBub21icmVzIGRlIGxhcyBmcnV0YXMgcXVlIG5vIHNvbiByb2phcywgeSAKICAgICAgICAgICAgLy8gcXVlIHN1IHByaW1lcmEgbGV0cmEgZXMgZGlzdGludGEgZGUgYHBgOgogICAgICAgICAgICBJRW51bWVyYWJsZTxzdHJpbmc+IHJlc3VsdGFkbyA9IGZyb20gZnJ1dGEgaW4gZnJ1dGFzIHdoZXJlIGZydXRhLkNvbG9yICE9ICJyb2pvIiAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICYmIGZydXRhLk5vbWJyZVswXSAhPSAncCcgb3JkZXJieSBmcnV0YS5Ob21icmUgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZWxlY3QgZnJ1dGEuTm9tYnJlOwogICAgICAgICAgIAogICAgICAgICAgICAvLyBWaXN1YWxpemFjacOzbiBkZSByZXN1bHRhZG9zOgogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXG5Db25zdWx0YSB1c2FuZG8gZXhwcmVzaW9uZXMgZXN0w6FuZGFyOiIpOwogICAgICAgICAgICBmb3JlYWNoKHN0cmluZyBub21icmUgaW4gcmVzdWx0YWRvKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXHRSZXN1bHRhZG86IHswfS4iLCBub21icmUpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBBaG9yYSB1c2Ftb3MgZXhwcmVzaW9uZXMgbGFtYmRhIHBhcmEgbGEgbWlzbWEgY29uc3VsdGE6CiAgICAgICAgICAgIHJlc3VsdGFkbyA9IGZydXRhcy5XaGVyZShmcnV0YSA9PiBmcnV0YS5Db2xvciAhPSAicm9qbyIgJiYgZnJ1dGEuTm9tYnJlWzBdICE9ICdwJykuCiAgICAgICAgICAgICAgICAgICAgICAgIE9yZGVyQnkoZnJ1dGEgPT4gZnJ1dGEuTm9tYnJlKS5TZWxlY3QoZnJ1dGEgPT4gZnJ1dGEuTm9tYnJlKTsKICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIFZpc3VhbGl6YWNpw7NuIGRlIHJlc3VsdGFkb3M6CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJcbkNvbnN1bHRhIHVzYW5kbyBleHByZXNpb25lcyBsYW1iZGE6Iik7CiAgICAgICAgICAgIGZvcmVhY2goc3RyaW5nIG5vbWJyZSBpbiByZXN1bHRhZG8pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJcdFJlc3VsdGFkbzogezB9LiIsIG5vbWJyZSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CiAgICAgICAgfQogICAgfQp9