Last active
December 18, 2015 07:59
-
-
Save leandrocustodio/5750259 to your computer and use it in GitHub Desktop.
Expressão regular LINQ para conversão de objetos. Para evitar trafegar dados excessivos em alguns casos podemos utilizar essa expressão regular para selecionar apenas as propriedades que desejamos trafegar.
This file contains hidden or 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
//classe Costumer | |
public class Customer | |
{ | |
public int Id {get;set;} | |
public string nome {get;set;} | |
public string sobrenome {get;set;} | |
public string CPF {get;set;} | |
public string Telefone {get;set;} | |
} |
This file contains hidden or 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
public CustomerJson{ | |
public int Id {get;set;} | |
public string nome {get;set;} | |
//expressão regular utilizando o Linq para converter tipos | |
public static Expression<Func<Custumer, CustomerJson>> SelectFromCustomers = i => new CustomerJson | |
{ | |
Id = i.Id, | |
nome = i.nome | |
}; | |
} |
This file contains hidden or 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
public ActionResult(){ | |
try | |
{ | |
//carrega a lista de clientes para uma IList | |
IList<Customers> _customers = db.Costumers.ToList(); | |
//Query com a chamada da expressão regular para a conversão de tipos | |
var customers = _customers.AsQueryable().Select(CostumersJson.SelectFromCustomers).ToList(); | |
return View(customers); | |
} | |
catch (Exception ex) | |
{ | |
List<CustomersJson> customers = new List<CustomersJson>(); | |
return View(customers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment