Felipe Maricato Moura
Felipe Maricato Moura

Reputation: 57

Passing object of anonymous type as parameter to a method

I need do something like 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 = from q in cars 
                      where q.nome.ToLower().Contains("eco") 
                      orderby q.nome 
                      select new { q.nome, q.id };

    doSomething(queryResult)
}

I need to pass the queryResult variable to function doSomething(). I tried use a dynamic type, List<T> object, but nothing works

public void doSomething(???? queryResult)
{
    foreach (carros ca in queryResult)
    {
        Response.Write(ca.nome);
        Response.Write(" - ");
        Response.Write(ca.id);
    }
}

Upvotes: 5

Views: 13157

Answers (4)

Reed Copsey
Reed Copsey

Reputation: 564323

In general, it's almost always a bad idea to pass anonymous types between methods.

You should make a custom type to hold the nome and id values, then construct it instead of an anonymous type.


In your case, you already have a class that would work: carros. You can just implement your query to create it instead:

var queryResult = from q in cars 
  where q.nome.ToLower().Contains("eco") 
  orderby q.nome 
  select new carros {q.nome, q.id};

Your method would then be:

public void doSomething(IEnumerable<carros> queryResults)

Upvotes: 11

Ed Chapel
Ed Chapel

Reputation: 6932

You cannot pass anonymous types* outside the scope of the current method. Use your carros type:

public void listar_carros()
{
    List<carros> Lcars = new List<carros>();
    Lcars.Add(new carros(1, "Fusca"));
    Lcars.Add(new carros(2, "Gol"));
    Lcars.Add(new carros(3, "Fiesta"));

    var qry = from q in Lcars
              where q.nome.ToLower().Contains("eco")
              orderby q.nome
              select q;

    doSomething(qry);

}

public void doSomething(IEnumerable<carros> qry)
{

    foreach (carros ca in qry)
    {
        Response.Write(ca.nome);
        Response.Write(" - ");
        Response.Write(ca.id);
        //Response.Write(ca);
        Response.Write("<br />");
    }
}

* You can pass them only as an object which is mostly useless since you would want to use them in strong-typed way.

Upvotes: 2

devshorts
devshorts

Reputation: 8872

Just wanted to chime in regarding passing anonymous types, while it's been mentioned that its bad juju to pass anonmyous types, you can... if you really wanted to (leveraging dynamic)

public void Anon()
{
     var x = new {foo = "bar"};

     AnonReciever(x);
}

public static void AnonReciever(dynamic o)
{
     Console.WriteLine(o.foo);
}

And this prints bar no problem.

I don't recommend it though. Best to use a custom typed object, which you already have.

Upvotes: 3

ChargerIIC
ChargerIIC

Reputation: 1690

Why not declare the new object as type carros?

var qry = from q in Lcars 
          where q.nome.ToLower().Contains("eco") 
          orderby q.nome 
          select new carros {q.nome, q.id};

Your parameter would then:

 IEnumerable{carros}

Upvotes: 2

Related Questions