LINQ 中的联接查询使用匿名类型时出错

发布于 2024-11-16 16:10:51 字数 1767 浏览 2 评论 0原文

这几天开始学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身软味 2024-11-23 16:10:51

字符串格式表达式中使用的是圆括号而不是大括号。

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

应该改为

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

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

Cesar 的电子邮件是[电子邮件受保护]

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]

桃酥萝莉 2024-11-23 16:10:51

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

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 }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文