I'm using Entity Framework 4.0. I'm connecting to a legacy DB (SQLAnywhere) and I want to convert some data in into a class. This class is also legacy code.
In the db-table, there is a column of type int16 and the class to convert into has the same field but of type uint16.
e.g.
Class to convert:
class Foo
{
UInt16 ID { get; set; }
string Name { get; set; }
}
In my database there is a table with elements of:
class FooDbItem
{
Int16 ID { get; set; }
string Name { get; set; }
int A { get; set; }
int B { get; set; }
}
Trying to get some selected data
var foos = db.FooDbItems.Select(f => new Foo
{
Name = f.Name,
ID = f.ID
});
var foos = db.FooDbItems.Select(f => new Foo
{
Name = f.Name,
ID = (Uint16)f.ID
});
Convert.ToUInt16 does not work either.
Because of the legacy code, I'm not able to just change the datatype.
The only solution I see at the moment is to get the data from the table as it is and iterate thru it and create a new list of the wanted datatype.
e.g.
List<Foo> foolist = new List<Foo>();
var dbfoos = db.FooDbItems;
foreach(var item in dbfoos)
{
Foo f = new Foo()
{
ID = (ushort)item.ID,
Name = item.Name
};
foolist.Add(f);
}
I tried google, but I think I asked the wrong question or I'm not seeing the solution. So any hints will be appreciated.
Thx
Andy