3

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 Answers 2

3

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
    };
Sign up to request clarification or add additional context in comments.

Comments

3
myDataContext.Mapping.GetTable (typeof (Customer)).RowType.DataMembers

3 Comments

how can I cast it to data column ?
You could do this: this.Mapping.GetTable (typeof (Customer)).RowType.DataMembers.Select (dm => new DataColumn (dm.Name, dm.Type) { AllowDBNull = dm.CanBeNull })
I got an exception : DataSet does not support System.Nullable<>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.