I have 2 objects like this:
public class Place : ICloneable, IEquatable<Place>
{
public string Name{ get; set; }
public float longitude{ get; set; }
public float latitude{ get; set; }
public Horizon thehorizon { get; set; }
...
}
public class Horizon
{
public List<PointF> points{ get; set; }
}
In my database I have 2 tables - "places" and "horizons", with a foreign key in horizons to know to which place the points belongs.
So the structure of the places is:
- Name -nvarchar - primary key
- longitude - real
- latitude - real
and the structure of the horizons is
- parent_key - nvarchar
- pointX - real
- poinY - real
I wrote the code below to select all the data and build a list of place. It's working, but it's very slow. If you have any suggestion how to make it faster (or any comment), please tell me.
DataTable TablePlaces;
DataTable TableHorizons;
public void init()
{
TablePlaces = new DataTable();
TablePlaces.Columns.Add("Name");
TablePlaces.Columns.Add("longitude");
TablePlaces.Columns.Add("latitude");
TableHorizons = new DataTable();
TableHorizons.Columns.Add("parent_key");
TableHorizons.Columns.Add("pointX");
TableHorizons.Columns.Add("pointY");
System.Data.DataSet DS = new DataSet();
DS.Tables.Add(TablePlaces);
DS.Tables.Add(TableHorizons);
DS.Relations.Add(TablePlaces.Columns["Name"],
TableHorizons.Columns["parent_key"]);
}
public List<Place> BuilsListPlace()
{
TableHorizons.Clear();
TablePlaces.Clear();
using (DbCommand Command = newConnectionNewCommand())
{
Command.CommandText = "SELECT * FROM places ORDER BY Name"
fill(TablePlaces, Command);
Command.CommandText = "SELECT * FROM horizons ORDER BY parent_key,pointX";
fill(TableHorizons, Command);
Command.Connection.Dispose();
}
return (from DataRow dr in TablePlaces.Rows
select newPlace(dr)).ToList();
}
void fill(TableDB t ,DbCommand Command)
{
using (var da = newDataAdapter())
{
da.SelectCommand = Command;
da.MissingSchemaAction = MissingSchemaAction.Ignore;
da.Fill(t);
}
}
Place newPlace(DataRow dr)
{
Place result = new Place();
result.longitude=(float)dr["longitude"];
result.latitude=(float)dr["latitude"];
result.Name=(string)dr["Name"];
result.theHorizon=newHorizon(dr.GetChildRows(dr.Table.ChildRelations[0]));
return result;
}
Horizon newHorizon(DataRow[] Rows)
{
Horizon result = new Horizon();
result.points = new List<PointF>();
foreach(DataRow dr in Rows)
result.points.Add(new PointF((float)dr["pointX"],(float)dr["pointY"]);
return result;
}
This extension I found for converting
datatabletolist of objectsusing reflection & generic, but keep in mind that the property name has to be exact the same with the one in datatableHope it helps