LINQ Anonymized Type as parameter

Asked

Viewed 131 times

2

I am trying to create a method that receives an anonymous type (LINQ query)
(Please correct me if the term tipo anônimo (anonymous types) is incorrect)

MinhaLista (produtos.Select(p => new {p.Id, p.Estoque})

I asked Visual Studio to create the method automatically and I was generated:
public static object MinhaLista (object @select)

I would like the method to be able to do:

foreach (var produto in produtos)
            {
                Debug.WriteLine(produto.Id);
            }

What is the correct way to define a method to receive this tipo anônimo (anonymous types)?

  • This is one way, but it depends on what you want to do. I don’t know if I can answer this just with this information.

  • I edited with what I would like to do in the method. @bigown

  • Now the question has become totally meaningless.

  • How could I have access to the data passed by MinhaLista (produtos.Select(p => new {p.Id, p.Estoque}) in my method MinhaLista (object @select) {}

  • your method has to be even Anonymous? , could not create a class that had the types you are going through?

  • 1

    According to your question, I believe my answer is correct. Because I’m showing you how to get properties from an anonymous object. In addition I still prove the operation in fiddle

Show 1 more comment

3 answers

3


I suggest you create a Extension Method of object to this end, since it will guarantee reuse, it follows the code of the Extension method:

public static class ObjectExtensions
{
    public static object GetPropriedade(this object obj, string propName)
    {
        if (obj== null)
            return null;
        PropertyInfo prop = obj.GetType().GetProperty(propName);
        if (prop != null)
           return prop.GetValue(obj, null);
        return null;
    }
}

Then just call on the object that owns the property:

public void MostrarPropriedade(object obj)
{
    Console.WriteLine(obj.GetPropriedade("Id"));
}

You can see an example in my Fiddle: https://dotnetfiddle.net/JXqzAM

1

In general it is not a good idea to try to pass an anonymous type between methods.

The ideal in this case is to create a new type to be used, for example

public class MeuProduto
{
    public int Id { get; set; }
    public int Estoque{ get; set; }
}

Then you could change your query to use this type

MinhaLista (produtos.Select(p => new MeuProduto{Id = p.Id, Estoque = p.Estoque})

After that you should be able to declare your function as

public static object MinhaLista (IEnumerable<MeuProduto> @select)
  • Although I follow your advice, I could not mark as an answer following the criteria of the question. Thanks.

0

Another way he found was by using IEnumerable<dynamic> (however, the ability to Intellisense for the attributes is lost).

{
   MinhaLista (produtos.Select(p => new {p.Id, p.Estoque})
}

public static object MinhaLista (IEnumerable<dynamic> produtos)
{
    foreach (var produto in produtos)
    {
        Console.WriteLine(produto.Id);
    }
    ....
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.