tags:

views:

69

answers:

2

How to get an array of data columns via LINQ To SQL? I'm looking for something like this :

DataColumn[] dc = DataContext.Table.Columns;  
+2  A: 
myDataContext.Mapping.GetTable (typeof (Customer)).RowType.DataMembers
Joe Albahari
how can I cast it to data column ?
LIX
You could do this: this.Mapping.GetTable (typeof (Customer)).RowType.DataMembers.Select (dm => new DataColumn (dm.Name, dm.Type) { AllowDBNull = dm.CanBeNull })
Joe Albahari
I got an exception : **DataSet does not support System.Nullable<>.**
LIX
+3  A: 

Try:

var dataColumns =
    from member in yourDataContext.Mapping.GetMetaType(typeof(YourMappedType)).DataMembers
    select new DataColumn {
        ColumnName = member.MappedName,
        DataType = (
            member.Type.IsGenericType && member.Type.GetGenericTypeDefinition() == typeof(Nullable<>)
                ? new NullableConverter(member.Type).UnderlyingType
                : member.Type
        ),
        AllowDBNull = member.CanBeNull
    };
Frédéric Hamidi