0

Possible Duplicate:
How do I map a char property using the Entity Framework 4.1 “code only” fluent API?

I have some code that requires I use a char in c#.

When I store the data in the database as char(1) and return it via Entity Framework, I get a string back.

I'm currently converting it simply by doing string[0]. Sorry if this is a bit of a noob question but is there a more concrete way of doing this?

Community
  • 1
  • 1
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103

3 Answers3

1

I think is perfect, a string is a char followed with a null byte, this way you only retrieve first char. It's fast and secure because if you receive an empty string, the char will be null and you can check it.

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
1

Under the curtain, this should be the fastest way, but you should probably use checks for null and .length == 0;

Ivan Koshelev
  • 3,830
  • 2
  • 30
  • 50
1

I believe that is one of the better ways to handle that situation. I needed to do that before, and I also had a check in there to make sure it wasn't null.

so

string[0]

is the easiest way to handle that. Just make sure you are checking for null and all that good stuff

AndyC
  • 1,325
  • 1
  • 13
  • 23