2009-12-15 70 views
2
select new FeedResource 
{ 
    Title = (string)details.Element("title"), 
    Host = (string)details.Element("link"), 
    Description = (string)details.Element("description"), 
    PublishedOn = (DateTime?)details.Element("pubDate"), 
    Generator = (string)details.Element("generator"), 
    Language = (string)details.Element("language") 
} 

裏面添加的功能在上面的代碼中,我想類型轉換值傳遞給另一個函數, 例如何選擇查詢

Description = getValidDescription((string) details.Element("description")) 

但我不能夠實現,任何投入?

注:需要類型轉換處理空值(即如果沒有價值目前爲「描述」它(的XElement)處理空完美

+1

有什麼錯誤 – itowlson 2009-12-15 03:47:10

+0

嘗試寫getValidDescription(string)方法,一切似乎OK – LorenVS 2009-12-15 04:00:06

+0

作爲itowlson問,什麼是錯誤的,因爲不應該有一個問題與調用其他方法的方法你想要的,也許你還應該包含你的getValidDescription方法(或者至少是s ignature)。幫助我們幫助你... – Kamal 2009-12-15 04:34:55

回答

0

工作對我很好,什麼是錯誤訊息?例如:?

// doesn't have to be static - just simpler for my test 
static string getValidDescription(string description) 
{ 
    // handle nulls safely (could return a default here) 
    if (description == null) return null; 
    // for example only... 
    return CultureInfo.CurrentCulture.TextInfo 
     .ToTitleCase(description); 
} 

var qry = 
    from details in doc.Root.Elements("detail") 
    select new FeedResource 
    { 
     Title = (string)details.Element("title"), 
     Host = (string)details.Element("link"), 
     Description = getValidDescription((string) details.Element("description")), 
     PublishedOn = (DateTime?)details.Element("pubDate"), 
     Generator = (string)details.Element("generator"), 
     Language = (string)details.Element("language") 
    };