2013-06-27 30 views
1

我有以下代碼,我需要修改爲group by offer,但我不確定如何在此查詢中添加group by部分。VB.Net和XDocument - Group By

任何想法?

propertyInfo = myXmlFile.Descendants("offer").Descendants("site") _ 
    .Where(Function(f) _ 
    f.Attributes("code").First.Value = Location.Text) _ 
    .Select(Function(f) New With { _ 
    .LOCATION = f.Attributes("code").First.Value, _ 
    .TITLE = f.Parent.Attributes("title").First.Value, _ 
    .OFFER = f.Parent.Attributes("offerid").First.Value, _ 
    .POPUPDESC = f.Parent.Descendants("short_desc").Value 

示例XML

<offers> 
    <offer title="Offer title" offerid="AS32"> 
    <short_desc><![CDATA[ 
     <div>Your HTML's body <a href='dfgdfgdfgdfg.html'>Offer title</a> 
     </div> 
    ]]></short_desc> 

    <site code="CO"> 
     <year_week_start>201344</year_week_start> 
     <year_week_end>201414</year_week_end> 
    </site> 

    <site code="FH"> 
     <year_week_start>201446</year_week_start> 
     <year_week_end>201450</year_week_end> 
    </site> 
    </offer> 
</offers> 
+0

你真的需要用LINQ方法的語法嗎?我認爲像分組查詢語法http://msdn.microsoft.com/en-us/library/bb531412.aspx這樣的任務更容易處理。而XML的樣本會有所幫助。你想按屬性'offerid'分組嗎? –

回答

0

我想你需要一點點在你想要什麼,你組後,更具體。這很容易進行分組,但是接下來你會得到一組可以枚舉的數組:

propertyInfo = myXmlFile.Descendants("offer").Descendants("site") _ 
    .Where(Function(f) _ 
    f.Attributes("code").First.Value = Location.Text) _ 
    .Select(Function(f) New With { _ 
    .LOCATION = f.Attributes("code").First.Value, _ 
    .TITLE = f.Parent.Attributes("title").First.Value, _ 
    .OFFER = f.Parent.Attributes("offerid").First.Value, _ 
    .POPUPDESC = f.Parent.Descendants("short_desc").Value) _ 
    .GroupBy(Function(f) f.OFFER)