这些天我开始学习 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 的电子邮件和学生的名字。我不能这样做???
提前致谢。
凯文