Get values from a dynamic list

Asked

Viewed 3,501 times

3

I have a list of the kind dynamic:

private List<dynamic> listServicos = new List<dynamic>();

And it’s being populated like this:

listServicos.Add(new { Codigo = txtCodServico.Text, Servico = txtNomeServico.Text, Quantidade = txtQuantidadeServico.Text, Preco = txtPrecoUniServico.Text });

I’d like to take the values of lista, but I’m not getting it. I was trying that way:

foreach (var listServico in listServicos)
{                
    var codigo = listServico.Codigo;               
}

Visual Studio shows error:

SS

  • 1

    What mistake is he accusing?

  • @Ricardo Pergunta editada!

  • Leonardo, can you tell us more about the project? I did a project here with a copy code of yours and it worked with all the frameworks I used, did you migrate from MVC 2 to 3 for example? Anything that’s been done to it could be the cause, because the code works...

  • @Ricardo now I was curious to know how his worked. All the tests I did didn’t work. See wrong: https://dotnetfiddle.net/7EyQ0n

  • Strange, in fiddle does not work, if you create a new console project on your machine and put exactly your code it works... I still believe the problem is something of the environment.

  • @Leonardo you really need the list to have dynamic elements, that is, they need to change their structure at runtime, or it was actually the solution you adopted to have an anonymous type, but it doesn’t need to be dynamic?

  • @bigown was actually a solution I adopted. It doesn’t have to be totally dynamic

  • @Leonardo I’m thinking I’ve already given you a better answer. dynamic should be used as a last resort. You have to make sure that the solution can only be given in Runtime.

Show 3 more comments

2 answers

4


I still haven’t found a more suitable solution, just one using ExpandoObject:

dynamic obj = new ExpandoObject();
obj.Codigo = "Código";
obj.Servico = "Serviço";
obj.Quantidade = "Quantidade";
obj.Preco = "Preço";
var listServicos = new List<dynamic>{ obj };
WriteLine(listServicos[0].Codigo);

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I wonder if you really need this. It may be that the solution is simpler than this. It is rare that someone needs something really dynamic.

This could be the solution:

var list = Enumerable.Empty<object>()
             .Select(r => new { Codigo = "Codigo", Servico = "Servico", Quantidade = "Quantidade", Preco = "Preco" })
             .ToList();
list.Add(new { Codigo = "Codigo", Servico = "Servico", Quantidade = "Quantidade", Preco = "Preco" });
WriteLine(list[0].Codigo);

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Another solution is to generalize rather than streamline:

public static void Main() {
    var list = ToAnonymousList(new { Codigo = "Codigo", Servico = "Servico", Quantidade = "Quantidade", Preco = "Preco" });
    WriteLine(list[0].Codigo);
}
public static IList<T> ToAnonymousList<T>(params T[] items) => items;

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I found these in the OS.

Without needing an auxiliary method:

var list = new[] { new { Codigo = "Codigo", Servico = "Servico", Quantidade = "Quantidade", Preco = "Preco" } }.ToList();
WriteLine(list[0].Codigo);

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

0

1 - Add the reference to Microsoft.Csharp.

If you already have the reference, remove and insert again.

Look that answer, I believe I can help you.

2 - Another possible solution is to change the framework from 4.5 to 4.5.1. (Source) Just go to Web.config and change:

<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />

for:

<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />

rebuild.

3 - Another attempt is to change the Copy Local = True for reference to Microsoft.CSharp

  • I added the reference and it hasn’t worked yet, and I can’t change the version of the project framework either =/

  • What is your current framework?

  • The framework version is 4.0

  • That’s not the problem. Is that the variable is not really dynamic, I’ll see if I can give a plausible answer.

  • Um, let’s wait for the @bigown response. I didn’t quite understand what the problem would be.

  • I’m waiting for @bigown

  • It’s hard to understand what’s wrong, but I won’t give up so easily.

  • I created a project from scratch here with your code, I managed to run with all the frameworks, try the number 3 I just put in the answer.

Show 3 more comments

Browser other questions tagged

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