using System.Collections.Generic;
public class DatabaseAnimal {
public int id { get; set; }
public string animal { get; set; }
public string type { get; set; }
animalType = new List<AnimalType>();
public string animalDesc { get; set; }
public List<AnimalType> animalType { get; set; }
public class AnimalType {
public string typeDesc { get; set; }
public static void Main()
var dbAnimal = DatabaseValues();
var result = dbAnimal.GroupBy(a => a.animal)
.Select(grp => new Animal {
animalType = grp.Select(e => new AnimalType { typeDesc = e.type }).ToList()
Console.Write(JsonConvert.SerializeObject(result, Formatting.Indented));
private static IEnumerable<DatabaseAnimal> DatabaseValues() {
yield return new DatabaseAnimal {
yield return new DatabaseAnimal {
yield return new DatabaseAnimal {
yield return new DatabaseAnimal {