Member 'string.Format (string, params object [])' cannot be accessed with reference to the instance; give it a type name instead

I have a page that displays a list of contracts. Each line contains the amount, fee and total amount that I calculate the line of this line.

I am also trying to calculate the total amount that sums Total from the collection.

Here is what I have:

var contracts = _contractsRepository.Contracts. Select(c => new ContractViewModel { ContractId = c.ContractID, Amount = c.Amount, Fee = c.Fee, Total = c.Sum(cc => cc.Amount) + c.Sum(cc => cc.AdminFee) }); // error here ViewBag.GrandTotal = contracts.Sum(c => c.Total).ToString().Format("{0:c}"); 

I get a compilation error when I try to calculate the total amount, which says:

Member 'string.Format (string, params object [])' cannot be obtained with reference to the instance; qualify it instead of type name

Does anyone know what I can do to fix this?

+6
source share
1 answer

You are mistaken in the lines.

String.Format() is a static method that takes a format string and a set of parameters.
You may call

 String.Format("{0:c}", someDecimal); 

However, if you have only one value, you can call ToString() with the format parameter:

 someDecimal.ToString("c") 
+7
source

All Articles