C# 如何在select查询中添加函数
在上面的代码中,我想将类型转换值传递给另一个函数, 范例 但我无法实现,有什么投入吗C# 如何在select查询中添加函数,c#,linq,function,select,C#,Linq,Function,Select,在上面的代码中,我想将类型转换值传递给另一个函数, 范例 但我无法实现,有什么投入吗 注意:类型转换需要处理空值,如果描述中没有值,那么XElement可以完美地处理空值。对我来说很好;错误消息是什么?例如: Description = getValidDescription((string) details.Element("description")) 为我工作很好;错误消息是什么?例如: Description = getValidDescription((string) details
注意:类型转换需要处理空值,如果描述中没有值,那么XElement可以完美地处理空值。对我来说很好;错误消息是什么?例如:
Description = getValidDescription((string) details.Element("description"))
为我工作很好;错误消息是什么?例如:
Description = getValidDescription((string) details.Element("description"))
试着编写getValidDescriptionstring方法,其他一切似乎都正常,正如itowlson所问的,错误是什么。因为以您想要的方式调用另一个方法不应该有问题。也许您还应该包括getValidDescription方法,或者至少包括签名。帮我们帮你…道歉。它正在工作。问题是许多参数传递给同一个方法,这引发了异常。其中一个确实具有字符串类型转换。请尝试编写getValidDescriptionstring方法,其他所有操作似乎都正常,正如itowlson所问的,错误是什么。因为以您想要的方式调用另一个方法不应该有问题。也许您还应该包括getValidDescription方法,或者至少包括签名。帮我们帮你…道歉。它正在工作。问题是许多参数传递给同一个方法,这引发了异常。其中一个有字符串类型的铸造。
// 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")
};