#Output
Pessoa[0].Nome=Alberto&Pessoa[0].Idade=22&Pessoa[1].Nome=Alberto 2&Pessoa[1].Idade=23
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| namespace ConsoleApplication7 | |
| { | |
| public class Pessoa | |
| { | |
| public string Nome { get; set; } | |
| public int Idade { get; set; } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var pessoas = new List<Pessoa> | |
| { | |
| new Pessoa { Nome = "Alberto", Idade = 22 }, | |
| new Pessoa { Nome = "Alberto 2", Idade = 23 } | |
| }; | |
| var pessoaProperties = typeof(Pessoa).GetProperties(); | |
| var valores = pessoas.SelectMany((pessoa, i) => pessoaProperties.Select(prop => string.Format("Pessoa[{0}].{1}={2}", i, prop.Name, prop.GetValue(pessoa, null)))); | |
| var @join = string.Join("&", valores); | |
| Console.WriteLine(@join); | |
| } | |
| } | |
| } |
#Output
Pessoa[0].Nome=Alberto&Pessoa[0].Idade=22&Pessoa[1].Nome=Alberto 2&Pessoa[1].Idade=23