0

LLBLGen を使用して DataTable から EntityCollection または List(Of Entity) を読み込むにはどうすればよいですか?

4

1 に答える 1

2

データテーブルはその値を行と列に保持しますが、LLBLGen Collection クラスは永続ストレージ内のテーブルを表す Entity オブジェクトのコレクションを保持します。TypedListDAO を介して、ResultsetFields で定義したフィールドの DataTable を取得できます。ただし、Entity オブジェクトが DataTable に格納されていない限り、DataTable から EntityCollection に移行することはできません。

多くの場合、DataTable にいくつかのキーがあります。この場合、DataTable の行を反復処理し、キーを取り出して、これらから新しい Entity オブジェクトを作成する必要があります。次に、これらの Entity オブジェクトを EntityCollection に追加できます。

// Define the fields that we want to get
ResultsetFields fields = new ResultsetFields(2);
fields.DefineField(EmployeeFields.EmployeeId, 0);
fields.DefineField(EmployeeFields.Name, 1);

// Fetch the data from the db and stuff into the datatable
DataTable dt = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, dt, 0, null, null, null, false, null, null, 0, 0);

// We now have a datatable with "EmployeeId | Name"
// Create a new (empty) collection class to hold all of the EmployeeEntity objects we'll create
EmployeeCollection employees = new EmployeeCollection();
EmployeeEntity employee;

foreach(DataRow row in dt.Rows)
{
    // Make sure the employeeId we are getting out of the DataTable row is at least a valid long
    long employeeId;
    if(long.TryParse(row["EmployeeId"].ToString(), out employeeId)) 
    {
        // ok-looking long value, create the Employee entity object
        employee = new EmployeeEntity(employeeId);

        // might want to check if this is .IsNew to check if it is a valid object
    }
    else
    {
        throw new Exception("Not a valid EmployeeId!");
    }

    // Add the object to the collection
    employees.Add(employee);
}

// now you have a collection of employee objects...
employees.DoStuff();
于 2011-06-06T02:32:10.880 に答える