Reputation: 2964
I have a LINQ query which creates an anonymous collection. One thing I'm returning is a number, which I'd like formatted as currency instead of 50.23888838222 etc. However if I try something like:
... select new { Amount = e.Item_Amount.ToString("C") }
I get told 'No overload for .ToString takes 1 arguments.
However this code works fine elsewhere when changing an individual string.
I've had issues like this with Linq to Sql and understand why, but this is just an in memory collection. What's the best way to achieve this end?
Thanks
Upvotes: 0
Views: 207
Reputation: 94645
Its nullable
field (Maybe).
Amount = e.Item_Amount.Value.ToString("C")
//or
Amount = (e.Item_Amount ?? 0).ToString("C");
Upvotes: 3
Reputation: 17498
It sounds like e.Item_Amount is not of a numeric type.
You can either change its type to a numeric type such as Int32 or decimal (preferable) or to parse the values during iteration.
select new {
Amount = decimal.Parse(e.Item_Amount).ToString("C")
}
If your dealing with a nullable type, you could do the following:
select new {
Amount = (e.Item_Amount ?? 0).ToString("C")
}
Upvotes: 0