linq to lambda expression
I am tryin开发者_运维百科g to convert the code below to a lambda expression
from b in bookResults
group b by new { b.Title1, b.Pubs_id, b.Notes } into g
select new XElement("book",
new XAttribute("pub_id", g.Key.Pubs_id), new XAttribute("note", g.Key.Notes),
new XElement("Title", g.Key.Title1),
from bk in g
select new XElement("Name", new XAttribute("au_id", bk.Au_id), bk.Name))));
lambda expression
bookResults.GroupBy(g => new { g.Title1, g.Pubs_id, g.Notes })
.Select(group => new XElement("book",
new XAttribute("pub_id",
group.Key.Pubs_id),
new XAttribute("note", group.Key.Notes),
new XElement("Title", group.Key.Title1)))
**.Select(bk => new XElement("Name",
new XAttribute("au_id",
bk.Au_id),
bk.Name)**)));
My issue is with the second select as I don't know how to associate it with
(from bk in g)That part is just yet another regular query, starting with the lambda parameter of the previous select:
bookResults.GroupBy(g => new { g.Title1, g.Pubs_id, g.Notes })
.Select(group => new XElement("book",
new XAttribute("pub_id", group.Key.Pubs_id),
new XAttribute("note", group.Key.Notes),
new XElement("Title", group.Key.Title1)),
// "from bk in g select" becomes "g.Select(bk =>"
// but here you're using group as the parameter name
group.Select(bk => new XElement("Name",
new XAttribute("au_id", bk.Au_id), bk.Name))));
精彩评论