tags:

views:

92

answers:

4

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?

+2  A: 

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
Its 4.0, not 3.5.
Will
thought I'd get a freebie since you were a moderator and it was in a comment =)
Micah
+2  A: 

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
+1  A: 

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 http://stackoverflow.com/questions/2552816/alternatives-to-inflector-net/2553071#2553071)

Rup
A: 

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
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
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
@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