如何使用myGeneration从BusinessEntity获取对象列表? [英] How to get list of objects from BusinessEntity using myGeneration?
问题描述
在某些项目中,我使用了 myGeneration ;我注意到,BusinessEntity将查询的结果保存在DataTable中,
但是,如果开发人员希望将结果作为对象列表获取该怎么办?
为解决此问题,我进行了以下修改以供自己使用。
I used myGeneration in some projects; I noticed that BusinessEntity keeps the result of the query in a DataTable, But what if the developer wants to get the result as List of objects? To solve this, I made the following modification for my own use.
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,并且为他生成了一个类,我还要进行以下修改:
Now suppose we have a table "Employee" and we generate a class for him, also I make the following modification:
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;
}
}
现在我可以从查询的结果,而不是一个对象和数据表中的结果:
Now I can Get a list of objects, from the result of the query, instead of one object and the result in the data table :
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);
}
然后,我可以按以下方式访问选定的员工:
Then I can access the selected Employee as following:
Employee emp = (Employee) mMainTree.SelectedNode.Tag;
emp.Name = "WWWWW";
emp.Save();
谢谢。
您有更好的提示吗?
有关更多讨论,请访问MyGeneration论坛。
thanks. Do You have a better tips, Ideas? for more discussion, please visit MyGeneration forum.
推荐答案
为什么要创建一个名为AsList()的方法,曾经退货吗?您可以创建一个像这样的通用扩展方法(从我的头顶创建。):
Why would you create a method called AsList() that only ever returns one Item? You could just create a generic extension method like this (created from the top of my head..):
public static List<T> AsList(this T item)
{
return new List<T>() { item };
}
没有必要遍历员工列表并将其添加到一个TreeNode。
There is no point to loop through the Employee List of one to add it to a TreeNode.
这篇关于如何使用myGeneration从BusinessEntity获取对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!