linq 使用group by的Lambda函数

tjrkku2a  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(135)
var result = booklist.GroupJoin(authorlist, authorlist2,
    x => x.ISBN,
    y => y.AuthorId,
    z => z.AuthorID,
    (x, y, z) => new
    {
        x.Book_Name,
        y.AuthorName,
        z.AuthorName
    }).GroupBy(od => new
{
    od.Book_Name,
    od.AuthorName
}).OrderBy(d => d.Key.Book_Name).Select(grp => new

{
    AuthorName = grp.Key.AuthorName,
    Book_Name = grp.Key.Book_Name,
});

foreach (var item in result)

{
    Console.WriteLine(string.Format(" BookName:-   {0}\n AuthorName:- {1}\n", item.Book_Name, item.AuthorName));
}

enter image description here
这里我使用lambda函数打印了一本书和两个作者名,但是在GroupJoin上显示了错误。请检查并告诉我哪里出错了

0lvr5msh

0lvr5msh1#

错误说明指出GroupJoin方法没有带六个参数的重载。GroupJoin只能通过两个导航属性联系两个列表,每个列表一个导航属性。
连接列表后,分组将如下所示:

var result = booklist.GroupJoin(concatenatedAuthorlist,
        left => left .AuthorId,  // navigation from booklist
        right => right .AuthorID,  // navigation from concatenatedAuthorlist
        (left, right) => new
        {
            left.Book_Name,
            left.AuthorName,
            right.AuthorName
        }).
        //omitted for brevity

相关问题