How to work with anonymous guy

Asked

Viewed 301 times

1

I have the following code snippet:

var filter = new { categoria = 1, cond2 = foo, ... };
p = new ProdutoBusiness().listarProdutos( filter ).ToList();

I wanted to work with this anonymous type as a filter parameter, but I don’t know how to receive this type of value in the reference method:

public IEnumerable<Produto> listarProdutos( tipo???? filter = null) {

My intention is to change the query according to what comes in the filter. For example:

if(filter != null){
   if(filter.categoria != ""){
     query = from P in con.produto
             join C in con.categoria on P.categoriaID equals C.categoriaID
             where P.ativo == true
             && P.categoriaID == filter.categoria
             orderby P.nome ascending
             select P;
   }
   [...]
 }

Any ideas/suggestions?

/*************/

If anyone has the same question with these guys 'Dynamic', mentioned in the reply, I resolved so:

Popular the object:

var filter = new { categoria = 1, cond2 = foo, ... }

Receiving the parameter:

public IEnumerable<Produto> listarProdutos( dynamic filter = null) {}

Getting the key value':

var fCategoria = filter.GetType( ).GetProperty( "categoria" ).GetValue( filter, null );
  • 1

    Anonymous type is impossible. You can use dynamic, but possibly, there are several better solutions than that. What’s the problem of creating a type for this?

1 answer

1


Since the exact type of the enumerable is not known a solution is to indicate to the compiler that it should not check the typing and leave for the run time to solve this. Is done with dynamic. Something like that:

public IEnumerable<Produto> listarProdutos(dynamic filter = null) {

But if you know it will always be this structure and it seems you know, then create a normal named type and use it in place of the anonymous type. The anonymous type is a facility when its structure doesn’t matter much for what it needs. Eventually it can even reuse an existing type. Maybe you really want to:

public IEnumerable<Produto> listarProdutos(Filter filter = null) {

And would have:

public class Filter  {
    public int categoria = 1;
    public ALgumTipoAqui cond2 = foo,
    ...
}

I put in the Github for future reference.

In C# 7 you can use a tuple which may be the most suitable mechanism.

But depending on the specific need there may be another solution, perhaps quite different from what you are currently thinking.

  • ah I understood the idea of creating the class for the filter, but in which layer would enter? in the model itself? as I said I’m beginner with C#, but I already appreciate the idea, already gave me a clear

  • @Fabiomarcell I do not know, does not have enough information for me to say something this. I do not know if I could do this in an answer. You gotta put it where it makes the most sense.

  • Ok, but already helped with the initial problem. Thank you very much =)

Browser other questions tagged

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