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)可以完美处理空值)。
最佳答案
对我来说很好错误消息是什么?例:
// 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")
};
关于c# - 如何在选择查询中添加功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1905025/