0

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.

0

5 Answers 5

3

Instead of selecting anonymous type use:

records.Select(r =>  r.FirstName + " " + r.LastName)

So your Console.WriteLine could be:

Console.WriteLine(string.Join(", ", records.Select(r => r.FirstName + " " + r.LastName)));
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that was quick and simple. Thank you. I apologize for asking such a simple question!
@JosuéMolina, you are welcome, and don't worry about the question being simple, you got so many alternatives now :)
2

Try this,

Console.WriteLine(string.Join(", ", records.Select(r => String.Format("{0} {1}", r.FirstName, r.LastName) ));

1 Comment

string.Format is considerably less efficient than a simple concatenation due to the need to do a replace. It will definitely work, but should be avoided in many cases.
1

You are recreating a dynamic object. I think you meant something like this:

    Console.WriteLine(string.Join(", ", records.Select(r => r.FirstName + " " + r.LastName)));

Comments

1

Just use inline lambda expression as below:

Console.WriteLine(string.Join(", ", records.Select(r => r.FirstName + " " + r.LastName));

Comments

1

I would suggest to have a ToString() method. This makes the approach more clean.

public class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public override string ToString() {
        return string.Format("{0} {1}", FirstName, LastName);
    }
}

and then

Console.WriteLine(string.Join(", ", records.Select(r => r.ToString())));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.