How to iterate through LinqToSql EnitySet columns by name

B

Bill McCormick

Hello,

I need a way to iterate through an Entity Sets columns by name, then
retrieve the data from each field. Then, I need to map the data into
fields of another object that has field names that are the same as the
DB field names.

Here's what I have so far:


A property of the DataContaxt that returns an Enumerable object of
column names:

public partial class SQLScrpDbDataContext
{
public IEnumerable<string> DspColumnNames {
get {
AttributeMappingSource ams = new AttributeMappingSource();
MetaModel metaModel = ams.GetModel(typeof(SQLScrpDbDataContext));
MetaTable metaTable = metaModel.GetTable(typeof(Dsp));
MetaType metaType = metaTable.RowType;
foreach (MetaDataMember dataMember in metaType.DataMembers) {
if(dataMember.DbType != null)
yield return dataMember.MappedName;
}
}
}
}

So that part works but here's where I'm stuck:

Inside a funtion that will complete the mapping ...
ScOptionalTktFields optionalXML = new ScOptionalTktFields();

I have a Dsp Entity object (DspRec) and I want to map each field to an
identically named field in an another object (optionalXML.) So instead
of having a long list of something that looks like this:

optionalXML.DspId = DspRec.DspId;

... and so on for about 50 fields ...

I could have something like this (making up some syntax to illustrate
what I'd like to achieve):

foreach (string name in this._db.DspColumnNames) {
optionalXML[name] = DspRec[name];
}


I'm using VS2010 Beta1 and .NET4.0 Beta1.

Thanks,

Bill
 
A

Alberto Poblacion

Bill McCormick said:
I have a Dsp Entity object (DspRec) and I want to map each field to an
identically named field in an another object (optionalXML.) So instead of
having a long list of something that looks like this:

optionalXML.DspId = DspRec.DspId;

... and so on for about 50 fields ...

I could have something like this (making up some syntax to illustrate what
I'd like to achieve):

foreach (string name in this._db.DspColumnNames) {
optionalXML[name] = DspRec[name];
}


You are going to need Reflection:

using System.Reflection;
....
Type type1 = DspRec.GetType();
Type type2 = optionalXML.GetType();
foreach (string name in this._db.DspColumnNames) {
PropertyInfo pi1 = type1.GetProperty(name);
object value = pi1.GetValue(DspRec, null);
PropertyInfo pi2 = type2.GetProperty(name);
pi2.SetValue(optionalXML, value, null);
}
 
B

Bill McCormick

Bill McCormick said:
I have a Dsp Entity object (DspRec) and I want to map each field to an
identically named field in an another object (optionalXML.) So instead
of having a long list of something that looks like this:

optionalXML.DspId = DspRec.DspId;

... and so on for about 50 fields ...

I could have something like this (making up some syntax to illustrate
what I'd like to achieve):

foreach (string name in this._db.DspColumnNames) {
optionalXML[name] = DspRec[name];
}


You are going to need Reflection:

using System.Reflection;
...
Type type1 = DspRec.GetType();
Type type2 = optionalXML.GetType();
foreach (string name in this._db.DspColumnNames) {
PropertyInfo pi1 = type1.GetProperty(name);
object value = pi1.GetValue(DspRec, null);
PropertyInfo pi2 = type2.GetProperty(name);
pi2.SetValue(optionalXML, value, null);
}

Thanks. That was close enough to get me here:

Type typeXML = optionalXML.GetType();

foreach (MetaDataMember column in this._db.DspMetaColumns) {
object value = column.MemberAccessor.GetBoxedValue(this.DspRec);
FieldInfo xmlField = typeXML.GetField(column.Name);
if(xmlField != null)
xmlField.SetValue(optionalXML, value.ToString());
}
 
Top