LINQ 中的联接查询使用匿名类型时出错
这几天开始学习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="[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);
在匿名类型中,我从 contactList 获取电子邮件和学生的名字。我不能这样做???
提前致谢。
凯文
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串格式表达式中使用的是圆括号而不是大括号。
应该改为
.当我进行此更改时,输出为:
You have a parenthesis instead of a curly bracket in your string format expression.
should instead be
. When I make this change, the output is:
我假设您收到错误“输入字符串的格式不正确”
Console.WriteLine("{0)'s email is {1}", c.First, c.Email);
^ 你的第一个参数有 ) 而不是 }
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 }