我在一些项目中使用了 myGeneration ;我注意到 BusinessEntity 将查询结果保存在 DataTable 中,但是如果开发人员希望将结果作为对象列表获取呢?为了解决这个问题,我做了以下修改以供自己使用。
public class BusinessEntity
{
//....
public DataTable TheDataTable
{
get
{
return _dataTable;
}
set
{
_dataTable = value;
}
}
public void PutTheRow(DataRow pRow)
{
DataRow newRow = _dataTable.NewRow();
newRow.ItemArray = pRow.ItemArray;
_dataTable.Rows.Add(newRow);
_dataRow = newRow;
//Rewind();
_dataTable.AcceptChanges();
}
.......
}
现在假设我们有一个表“Employee”,我们为他生成一个类,我也做了以下修改:
public abstract class Employee : _Employee
{
public Employee()
{
}
public List<Employee> AsList()
{
List<Employee> list = new List<Employee>();
Employee businessEntity = null;
foreach (System.Data.DataRow row in TheDataTable.Rows)
{
businessEntity = new Employee();
businessEntity.TheDataTable = TheDataTable.Clone();
businessEntity.PutTheRow(row);
list.Add(businessEntity);
}
return list;
}
}
现在我可以从查询结果中获取对象列表,而不是一个对象和数据表中的结果:
Employee employee = new Employee();
employee.Where.ID.Operator = WhereParameter.Operand.NotIn;
employee.Where.ID.Value = "1,2,3";
employee.Query.Load();
foreach (Employee employeeLoop in employee.AsList())
{
TreeNode node = new TreeNode(employeeLoop.s_ID);
node.Tag = employeeLoop;
mMainTree.Nodes.Add(node);
}
然后我可以访问选定的员工,如下所示:
Employee emp = (Employee) mMainTree.SelectedNode.Tag;
emp.Name = "WWWWW";
emp.Save();
谢谢。你有更好的建议吗?如需更多讨论,请访问 MyGeneration 论坛。