Here is my LINQ Query:
GridView.DataSource = from it in DataContext.Items
where it.ThingId == thing.ThingId
select new
{
it.Something,
it.SomethingElse,
it.AnotherSomething,
Currency = String.Format("{0:C}", it.Decimal.ToString()),
//^--This is the line that needs to be converted to currency, this doesn't work--^
};
The it.Decimal is returning a decimal in the form 12345.00 and I need to convert that to $12,345.00 when displayed in the gridview. What I have now doesn’t work, because a LINQ Query won’t recognize the String.Format function. Any ideas?
Just remove the
ToString:You can also use this form:
If you pass
it.Decimal.ToString()as a parameter toString.Format, it will be handled as a string, not a decimal, so it won’t be able to apply the currency format.EDIT: ok, so you have another problem… Don’t forget that a Linq to SQL (or Entity Framework) query is converted to SQL and executed by the database; the database doesn’t know
String.Format, so it can’t be converted to SQL. You need to retrieve the “raw” result from the db, and then format it using Linq to Objects:(Note the use of
AsEnumerableto “switch” to Linq to Objects)Another option would be to give the raw decimal value to the
GridView, and set the column’sDataFormatStringproperty to “C”