A common problem – you have a number of puppy objects you need to display. But if if the number is greater than one then you need to display puppy not puppies. Solving this issue with a massive plural dictionary lookup or a smart english rules engine would have you walking on the road to code hell. So what can we do?

Welcome to Pete's Pet Store

You have @Model.PetType.GetDescription().ToPlural(Model.PetCount, outputCount: true) in your basket

In all 4 projects I’m the main BlueChilli developer for (Housl, Recruit Write Now, Brand Honee, and MoneySoft) this is in the top 3 helper functions that really make me smile!

/// Return the plural form of a word if count is not 1 
/// Word to pluralise
/// Number of items that represent word
/// Optionally return word prefixed with count
public static string ToPlural(this string s, int count = 0, bool outputCount = false)
    if (count != 1)
        s = PluralizationService.CreateService(new CultureInfo("en-US")).Pluralize(s);
    return (outputCount) ? String.Format("{0} {1}", count, s) : s;

How easy is that! Thanks to the System.Data.Entity.Design.dll in .Net a tiresome task made easy. Just the way I like it.

Filed under:   .net   c#   code   dave   development   function   helper   software   tips