Link to home
Start Free TrialLog in
Avatar of Scott Baldridge
Scott Baldridge

asked on

With linq select new list containing comma seperated list

Hello, I have a list of school objects that contain a list teacher objects from this I need to select a new object which contains the school-name, City, comma separated teacher-email

School
name
address
city
List<Teacher>

Teacher
name
email
grade

How can this be accomplished using Linq to query the list of schools?
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Not via Lynq but Just loop through teachers and build email string
string emails = "";
foreach (Teacher teacher in School.Teacher)
{
    If (emails != "")
    {
        emails = emails + "," + teacher.email;
    }
    else 
    {
        emails = teacher.email;
    }
}

Open in new window

I know you can do it via Lynq but against a string array...
string[] words = { "word1", "word2", "word3" };
string combined= words.Aggregate((current, next) => current + ", " + next);
Console.WriteLine(combined);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Both of these solutions meet the requirements of the question.