views:

90

answers:

3

with linq to sql(dbml file), how to get the size of a datatype?

for an example, varchar(50), how to get that 50?

thanks

A: 

You could grab the string (ex: "NVarChar(30)") from the ColumnAttribute associated with the property.

var prop = typeof(User).GetProperty("FirstName");
var attr = (ColumnAttribute)prop.GetCustomAttributes(typeof(ColumnAttribute), false)[0];
string dbType = attr.DbType;
int index = dbType.IndexOf("(") + 1;
int width = int.Parse(dbType.Substring(index, dbType.IndexOf(")") - index));
AgileJon
A: 

You can also do it like this...

    String databaseType = 
        *DataContextInstance*.Mapping.MappingSource
        .GetModel(typeof(*DataContext*))
        .GetMetaType(typeof(*Entity*))
        .DataMembers.First(x => x.Name.Equals("ColumnName"))
        .DbType;

Hope that helps :)

Chalkey
A: 

I used that method

Fredou