Advertisement
hhac

Busqueda general con listas

Aug 13th, 2019
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3.  
  4. public class BusquedaCliente
  5. {
  6.     public static void Main()
  7.     {
  8.         List<Cliente> listaClientes = new List<Cliente>();
  9.         listaClientes.Add(new Cliente() { Nombre = "Jorge", Apellido = "Lopez", NombreComercial = "Jorge Lopez", RFC = "LOJR800320AJM"});
  10.         listaClientes.Add(new Cliente() { Nombre = "Juan", Apellido = "Soto", NombreComercial = "Johnny Soto", RFC = "SOJU840922FJA"});
  11.         listaClientes.Add(new Cliente() { RazonSocial = "Radiodipsa Jorge Movil", NombreComercial = "Telcel PCS", RFC = "MOV920312FJA"});
  12.        
  13.         string filtros = "Hector Radio Movil";
  14.        
  15.         Console.WriteLine("Filtrado 2...");
  16.        
  17.         List<Cliente> filtro = TestDestructors.GetClientes(listaClientes, filtros);
  18.        
  19.         foreach(var c in filtro)
  20.         {
  21.             Console.WriteLine(c.NombreComercial);
  22.         }
  23.     }
  24.    
  25.     public static List<Cliente> GetClientes(List<Cliente> clientes, string filtros)
  26.     {
  27.         Console.WriteLine(clientes == null);
  28.         Console.WriteLine(filtros == null);
  29.         Console.WriteLine(filtros.Split(' ') == null);
  30.        
  31.         List<Cliente> resultado = new List<Cliente>();
  32.         foreach (var cliente in clientes)
  33.         {
  34.             foreach(var filtro in filtros.Split(' '))
  35.             {
  36.                 if ( !string.IsNullOrWhiteSpace(cliente.Nombre)  && cliente.Nombre.ToLower().Contains(filtro.ToLower())
  37.                    || !string.IsNullOrWhiteSpace(cliente.Apellido) && cliente.Apellido.ToLower().Contains(filtro.ToLower())
  38.                    || !string.IsNullOrWhiteSpace(cliente.RazonSocial) && cliente.RazonSocial.ToLower().Contains(filtro.ToLower())
  39.                    || !string.IsNullOrWhiteSpace(cliente.NombreComercial) && cliente.NombreComercial.ToLower().Contains(filtro.ToLower())
  40.                    || !string.IsNullOrWhiteSpace(cliente.RFC) && cliente.RFC.ToLower().Contains(filtro.ToLower()))
  41.                 {
  42.                     if (resultado.Find(w => w.NombreComercial == cliente.NombreComercial) == null)
  43.                         resultado.Add(cliente);
  44.                 }
  45.             }
  46.         }
  47.         return resultado;
  48.     }
  49.    
  50.     public class Cliente {
  51.         public string Nombre { set; get; }
  52.         public string Apellido { set; get; }
  53.         public string RazonSocial { set; get; }
  54.         public string NombreComercial { set; get; }
  55.         public string RFC { set; get; }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement