1

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

1
  • @Vincent >> Thank you for your question. Sorry about it. The error is from another line , not from linq !!! Error solved. So sorry to bother you . Commented Jun 23, 2011 at 7:18

2 Answers 2

3

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]

Sign up to request clarification or add additional context in comments.

3 Comments

OH So unfair. 1 minute before you and you get a +1 :) +1 from me too.
@Phill True, but I need the rep more than you do! :D +1 for you from me as well.
I know, I just thought it was funny :) your answer was better explained than mine so you deserve the accept more than me.
2

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 }

1 Comment

I gave u + 1 too. Sry I happened to see vincent's comment first somehow !!! Thx again!!!

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.