I tried the following:
var Title = LongTitle.Substring(0,20)
This works but not if LongTitle has a length of less than 20. How can I limit strings to a maximum of 20 characters but not get an error if they are just for example 5 characters long?
If the string Length is bigger than 20, use 20, else use the Length.
You can use the StringLength attribute. That way no string can be stored that is longer (or shorter) than a specified length.
See: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute%28v=vs.100%29.aspx
Make sure that length won't exceed
LongTitle
(null
checking skipped):This can be turned into extension method:
Which can be used as:
Shortest, the: