5
I have a method that should return a collection of anonymous objects:
/*Aqui deveria ser o tipo anonimo "AnonymousType"*/
[AnonymousType] ListarAnonimo()
{
//Especifica um "template" para o tipo retornado.
var lista = new[]
{
new
{
Nome = "",
Idade = 0,
Salario = 0.0m
}
}.ToList();
lista.Clear();
//Adiciona um item.
lista.Add(new
{
Nome = "Gato",
Idade = 25,
Salario = 3000000.0m
});
return lista;
}
I tried to get him to return a type list List<dynamic>
however in this way I received the following build error:
Error CS0029 Cannot implicitly Convert type 'System.Collections.Generic.List<>' to 'System.Collections.Generic.List'
So I tried to use the List<object>
but I got the same mistake.
Question
I’d like to know how I can returns an anonymous type Anonymoustype by a method?
It’s hard to answer that. Is there any pattern in the types that will be returned?
– Jéf Bueno
Returned types represent a database table.
– gato
And what is the way you are trying to use the return of this method? I think return a list of
object
should work.– Jéf Bueno
@jbueno the return is the fields q are being selected through a query using Dapper to return a
IEnumerable<dynamic> resultado
however need to return a typed list to popular a Datagridview through the propertyDataSource
.– gato