How to get list of objects from BusinessEntity using myGeneration?
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();
}
.......
}
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(employeeL开发者_JS百科oop.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();
thanks. Do You have a better tips, Ideas? for more discussion, please visit MyGeneration forum.
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 };
}
There is no point to loop through the Employee List of one to add it to a TreeNode.
精彩评论