C# condition adjustment

I have this condition here:

<td>$@(inventory.Elevations.FirstOrDefault(x => x.Title == e)?.Price.ToString("N0") ?? "Not Available")</td>

This works very well but if x.Title is not equal to e the dollar sign at the beginning is still appearing, I only want the dollar sign to appear if x.Title is equal to e...how would I adjust this so the dollar sign only appears with the Price and not with "Not Available"

1 answer

  • answered 2021-02-21 16:46 Hardik Shah

    You can use the below method,

    <td>@(inventory.Elevations.FirstOrDefault(x => x.Title == e)?.Price.ToString("C0") ?? "Not Available")</td>

    C0 - this will change the string to currency format.

    I hope this helps.