How can I a开发者_StackOverflow社区utomatically .Trim() whitespace from the results of my Linq2SQL query?
It seems that if SQL has a varchar width of 255 my returned result for "abc" will have 252 chars of whitespace.
Are using char(255)
rather than varchar(255)
?
If not, check the data in your database - you must be storing all those spaces in the column. Linq-to-sql will only return the column as a string. It does not pad it with spaces, and will only return the 252 spaces if they exist in your database. Are you storing all those spaces in the database? e.g. "abc______________"
I'd firstly suggest you fix your database, but if you can't do that then you can edit the generated code as Exoas suggests.
Try casting as a string:
(string)abc.Trim()
A Quick and Dirty way to make sure the fields are trimmed from your query's automatically, is to modify the designer generated getters for the fields you want trimmed to call the trim method.
get
{
return this._sometext.Trim();
}
Downside is that if you change the mappings it will be generated.
精彩评论