7

I have a page that displays a list of contracts. Each row contains an Amount, a Fee, and a Total that I'm calculating row that row.

I'm also trying to calculate a grand total, that sums up Total from the collection.

Here's 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 grand total that says:

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

Anyone know what I can do to fix this?

4

1 回答 1

9

You're misunderstanding strings.

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

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

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

someDecimal.ToString("c")
于 2013-03-31T02:01:21.470 回答