Last active
February 6, 2019 00:14
-
-
Save jfbueno/ad5f69ee217149b0b065f9649c76f8fd to your computer and use it in GitHub Desktop.
filtro-meuwhere created by jfbueno - https://repl.it/@jfbueno/filtro-meuwhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using static System.Console; | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
var numeros = Enumerable.Range(1, 31); | |
var maioresQue10 = numeros.MeuWhere(x => x > 10); | |
foreach(var n in maioresQue10) | |
WriteLine(n); | |
var usuarios = new[] | |
{ | |
new Usuario { Nome = "Jéferson", DataRegistro = new DateTime(2017, 5, 1) }, | |
new Usuario { Nome = "Mário", DataRegistro = new DateTime(2018, 2, 10) }, | |
new Usuario { Nome = "Juliana", DataRegistro = new DateTime(2019, 1, 10) }, | |
}; | |
WriteLine("\nUsuários registrados em 2018 ou depois"); | |
foreach(var u in usuarios.MeuWhere(x => x.DataRegistro.Year >= 2018)) | |
WriteLine($"{u.Nome} registrado em {u.DataRegistro:dd/MM/yyyy}"); | |
WriteLine("\nUsuários cujo nome inicia com a letra J"); | |
foreach(var u in usuarios.MeuWhere(x => x.Nome.StartsWith("J"))) | |
WriteLine(u.Nome); | |
} | |
} | |
static class MinhasExtensoes | |
{ | |
public static IEnumerable<T> MeuWhere<T>(this IEnumerable<T> src, Func<T, bool> predicate) | |
{ | |
var saida = new List<T>(); | |
foreach(var item in src) | |
if (predicate(item)) | |
saida.Add(item); | |
return saida; | |
} | |
} | |
class Usuario | |
{ | |
public string Nome { get; set; } | |
public DateTime DataRegistro { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment