这些天我开始学习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和学生的名字收到电子邮件。我不能这样做???
提前致谢。
凯文
答案 0 :(得分:3)
您的字符串格式表达式中有一个括号而不是一个大括号。
"{0)'s email is {1}"
应该是
"{0}'s email is {1}"
。当我进行此更改时,输出为:
Cesar的电子邮件是CesarGar@Contoso.com
答案 1 :(得分:2)
我将假设您收到错误“输入字符串格式不正确”
Console.WriteLine("{0)'s email is {1}", c.First, c.Email);
^你的第一个参数有a)not}