c# - Passing object of anonymous type as parameter to a method -
i need this:
public class carros { public int id { get; set; } public string nome { get; set; } } public void listar_carros() { list<carros> cars = new list<carros>(); cars.add(new carros{ id= 1, nome = "fusca" }); cars.add(new carros{ id= 2, nome = "gol" }); cars.add(new carros{ id= 3, nome = "fiesta" }); var queryresult = q in cars q.nome.tolower().contains("eco") orderby q.nome select new { q.nome, q.id }; dosomething(queryresult) } i need pass queryresult variable function dosomething(). tried use dynamic type, list<t> object, nothing works
public void dosomething(???? queryresult) { foreach (carros ca in queryresult) { response.write(ca.nome); response.write(" - "); response.write(ca.id); } }
in general, it's bad idea pass anonymous types between methods.
you should make custom type hold nome , id values, construct instead of anonymous type.
in case, have class work: carros. can implement query create instead:
var queryresult = q in cars q.nome.tolower().contains("eco") orderby q.nome select new carros {q.nome, q.id}; your method be:
public void dosomething(ienumerable<carros> queryresults)
Comments
Post a Comment