我有以下 Linq 语句:
var total = (from a in mobileChunk.Data select a.callCost).Sum();
Run Code Online (Sandbox Code Playgroud)
callCost是一个字符串。我需要将其转换为十进制。这是怎么做到的?
我会做这样的事情......
public static class Extenders
{
public static decimal ToDecimal(this string str)
{
// you can throw an exception or return a default value here
if ( string.IsNullOrEmpty(str) )
return someDefaultValue;
decimal d ;
// you could throw an exception or return a default value on failure
if ( !decimal.TryParse(str, out d ) )
return someDefaultValue;
return d;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在你的 linq 中......
var total = = (from a in mobileChunk.Data select a.callCost.ToDecimal()).Sum();
Run Code Online (Sandbox Code Playgroud)