3

In .net 3.5 if I generate a linq to sql data context, it does some wonderful magic to pluralize names. In my code I need to pluralize some terms. Can I use whatever method Linq is using to generate my plurals?

Mr Bell
  • 9,228
  • 18
  • 84
  • 134
  • 1
    Dunno about 3.5 but if I were targeting 4 I'd check out EF4's [PluralizationService](http://msdn.microsoft.com/en-us/library/system.data.entity.design.pluralizationservices.pluralizationservice.createservice.aspx) –  Aug 17 '10 at 17:14
  • Bummer. I am stuck on 3.5 for the moment – Mr Bell Aug 17 '10 at 19:24

4 Answers4

2

You'd have to use reflector to dig into the visual studio assemblies that do the code generation for the linq-to-sql designer.

Micah
  • 111,873
  • 86
  • 233
  • 325
2

LINQ to SQL doesn't expose its pluralization logic. A quick check with Reflector reveals that it's not using a terribly complex algorithm though:

If it ends with 'x', 'ch', 'sh' or 'ss', then add 'es'.
If it ends with 'y' preceded by a consonant, remove the y and add 'ies'.
Otherwise, add 's'.

If .NET 4 is an option, then EF's PluralizationService is much more thorough. Just in case you ever need to pluralize 'pneumonoultramicroscopicsilicovolcanoconiosis'.

stevemegson
  • 11,843
  • 2
  • 38
  • 43
2

There's also a .NET port of Inflector which does the same job. The author's blog is now down but it's available at http://cid-net.googlecode.com/svn/trunk/src/Cid.Mvc/Inflector.cs amongst others.

(via Alternatives to Inflector.Net)

Community
  • 1
  • 1
Rup
  • 33,765
  • 9
  • 83
  • 112
0

LINQ to SQL uses a fairly simple system for pluralization. If you are going to be working with complex terms, I recommend you use something like the Inflector that is part of SEDE. That piece of code seems to originate from the SubSonic project.

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • Inflector is not part of subsonic that seems to be a grievously incorrect attribution. It was written by Andrew Peters as a port from the ruby inflector. His site is no longer available however his code is accessible still at: http://cid-net.googlecode.com/svn/trunk/src/Cid.Mvc/Inflector.cs and correctly has his copyright notice included. – Chris Marisic Aug 17 '10 at 18:28
  • Actually, I'd say that the version in subsonic is an independent port of the Rails version, since they make different decisions about how to name things after porting. – stevemegson Aug 17 '10 at 19:47
  • @stevemegson that's true, the names are different. However, it seems that subsonic took Andrew's version, renamed all of the methods, and added brief XML documentation. – Maxim Zaslavsky Aug 17 '10 at 21:40