Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active December 14, 2015 12:59
Show Gist options
  • Save AlbertoMonteiro/5090337 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/5090337 to your computer and use it in GitHub Desktop.
Url encoded format
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment