我正在寫一個prgm,通過ADO datacontext,我在使用group clase的數據庫上運行查詢並返回一個xml文檔。我能夠獲得XML工作,但XAttribute("pub_id", from p in g select new { p.Pubs_id })
所賜:ADO Linq to xml
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[Pubs.BookStore+BookResults,<>f__AnonymousType1`1[System.String]]
下面
是代碼:
XDocument xdoc = new XDocument(new XElement("reports",
from b in bookResults
group b by b.Title1 into g
select new XElement("book",
new XAttribute("pub_id", from p in g select new { p.Pubs_id }),
new XElement("Title", g.Key),
from bk in g
select new XElement("Name", new XAttribute("au_id", bk.Au_id), bk.Name))
)
xdoc.Save("Books.xml");
示例XML輸出所需的(使用pubs示例數據庫)
<reports>
<book pub_id="1389">
<Title>The Busy Executive's Database Guide</Title>
<Name au_id="409-56-7008">Bennet,Abraham</Name>
<Name au_id="213-46-8915">Green,Marjorie</Name>
</book>
<book pub_id="0877">
<Title>Fifty Years in Buckingham Palace Kitchens</Title>
<Name au_id="648-92-1872">Blotchet-Halls,Reginald</Name>
</book>
<book pub_id="0877">
<Title>The Gourmet Microwave</Title>
<Name au_id="722-51-5454">DeFrance,Michel</Name>
<Name au_id="899-46-2035">Ringer,Anne</Name>
</book>
你說的話讓我思考,看來我需要PUB_ID爲重點的一部分,我做了以下,並通過新的工作 – SKatz
B組{b.Title1,b.Pubs_id} into g – SKatz
thnks for your help – SKatz