开发者

Can I use linq's method of pluralizing a term?

开发者 https://www.devze.com 2023-01-11 18:59 出处:网络
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 t

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?


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'.


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


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消