When using Linq to SQL and stored procedures, the class generated to describe the proc's result set uses char properties to represent char(1) columns in the SQL proc. I'd rather these be strings - is there any easy way to make this happen?
views:
467answers:
4Not sure why you'd want to do that. The underlying data type can't store more than one char, by representing it as a string variable you introduce the need to check lengths before committing to the db, where as if you leave it as is you can just call ToString() on the char to get a string
var whatever =
from x in something
select new { yourString = Char.ToString(x.theChar); }
You could modify the {database}.designer.cs file. I don't have one handy to check, but I believe it's fairly straight forward --- you'll just have to plow through a lot of code, and remember to re-apply the change if you ever regenerate it.
Alternately, you could create your own class and handle it in the select. For example, given the LINQ generated class:
class MyTable
{ int MyNum {get; set;}
int YourNum {get; set;}
char OneChar {get; set;}
}
you could easily create:
class MyFixedTable
{ int MyNum {get; set;}
int YourNum {get; set;}
string OneChar {get; set;}
public MyFixedTable(MyTable t)
{
this,MyNum = t.MyNum;
this.YourNum = t.YourNum;
this.OneChar = new string(t.OneChar, 1);
}
}
Then instead of writing:
var q = from t in db.MyTable
select t;
write
var q = from t in db.MyTable
select new MyFixTable(t);
Just go to the designer window and change the type to string. I do it all the time, as I find Strings to be easier to use than Char in my code.