kevin
kevin

Reputation: 14095

error when anonymous type is used in join query in LINQ

I start learning LINQ these days. I got an error when I try to return an anonymous type in join query. And I don't understand why I get this error. Here is my coding.

List<Student> students = new List<Student>()
        {
            new Student{First="Svetlana",Last="Omelchenko",ID=111,Scores= new List<int>(){97,92,81,60}},
            new Student{First="Claire",Last="O'Donnell",ID =112,Scores=new List<int>(){75,84,91,39}},
            new Student{First ="Sven",Last="Mortensen",ID =113,Scores=new List<int>(){88,94,65,91}},
            new Student{First ="Cesar",Last="Garcia",ID=114,Scores=new List<int>(){97,89,85,82}}
        };

    List<ContactInfo> contactList = new List<ContactInfo>()
    {
        new ContactInfo{ID=111,Email="Contoso",Phone= "206-555-0108"},
        new ContactInfo{ID=112,Email="[email protected]",Phone="206-555-0298"},
        new ContactInfo{ID=113,Email="[email protected]",Phone="206-555-1130"},
        new ContactInfo{ID=114,Email="[email protected]",Phone="206-555-0521"}
    };

var cInfo =
            from student in students
            where student.Scores.Average() > 85
            join ci in contactList on student.ID equals ci.ID
            select new { ci.Email, student.First };
        foreach (var c in cInfo)
            Console.WriteLine("{0)'s email is {1}", c.First, c.Email);

In the anonymous type, I get the email from contactList and student's first name. I can not do like this ???

Thanks in advance.

Kevin

Upvotes: 1

Views: 156

Answers (2)

Brennan Vincent
Brennan Vincent

Reputation: 10675

You have a parenthesis instead of a curly bracket in your string format expression.

"{0)'s email is {1}"

should instead be

"{0}'s email is {1}"

. When I make this change, the output is:

Cesar's email is [email protected]

Upvotes: 3

Phill
Phill

Reputation: 18814

I'm going to assume that you get the error "Input string is not in the correct format"

Console.WriteLine("{0)'s email is {1}", c.First, c.Email);

^ Your first parameter has a ) not }

Upvotes: 2

Related Questions