1

这些天我开始学习 LINQ。当我尝试在连接查询中返回匿名类型时出现错误。而且我不明白为什么会出现此错误。这是我的编码。

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="ClaireO@Contoso.com",Phone="206-555-0298"},
        new ContactInfo{ID=113,Email="SvenMort@Contoso.com",Phone="206-555-1130"},
        new ContactInfo{ID=114,Email="CesarGar@Contoso.com",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);

在匿名类型中,我收到来自contactList 的电子邮件和学生的名字。我不能这样做???

提前致谢。

凯文

4

2 回答 2

3

您的字符串格式表达式中有一个括号而不是大括号。

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

应该改为

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

. 当我进行此更改时,输出为:

Cesar 的电子邮件是 CesarGar@Contoso.com

于 2011-06-23T07:17:54.253 回答
2

我将假设您收到错误“输入字符串的格式不正确”

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

^ 你的第一个参数有 ) 不是 }

于 2011-06-23T07:16:56.500 回答