2011-08-29 150 views
2

我试图将代码转换下面lambda表达式LINQ到lambda表达式

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表达式

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)**))); 

我的问题是第二选择,因为我不知道该怎么将它与
(from bk in g)

+0

这不是一个lambda表达式。这是方法调用语法中的LINQ查询。 lambda表达式就像'x => blah'。 –

回答

1

该部分仅仅是另一个常规查询,从上一个select的lambda参数开始:

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))));