2009-12-15 49 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") 
    };