Take the following program:
using System;
using System.Collections.Generic;
using System.Linq;
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
var records = new Person[]
{
new Person{ FirstName = "John", LastName = "Doe", Age = 32 },
new Person{ FirstName = "Jane", LastName = "Doe", Age = 27 },
new Person{ FirstName = "Joe", LastName = "College", Age = 28 }
};
Console.WriteLine(string.Join(", ", records.Select(r => new
{
FullName = r.FirstName + " " + r.LastName
})));
}
}
Its expected output is:
John Doe, Jane Doe, Joe College
But its actual output is:
{ FullName = John Doe }, { FullName = Jane Doe }, { FullName = Joe College }
Is it possible to resolve this from inside the WriteLine?
This is a simplification of a bigger problem I recently encountered, and I need to resolve this from inside the WriteLine because I am performing this "serialization" inside a query; I cannot execute more than one statement.