4

我有一个函数可以获取数据库中的数据。下面是代码。

        public DataTable getAllTransaction(OleDbConnection conn)
        {
            OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
            string query = "";
            DataTable tblResult = new DataTable();

            query = @"SELECT t.id AS `Transaction ID`,
                                  c.id AS `Client ID`,
                                  c.clientname AS `Client Name`,
                                  t.cashvalue AS `Cash Value`,
                                  t.amount AS `Amount`,
                                  t.transdate AS `Transaction Date`,
                                  t.remarks AS `Remarks`
                             FROM client AS c 
                            INNER JOIN `transaction` AS t 
                               ON c.id=t.clientid";
            oleAdapter.SelectCommand = new OleDbCommand(query, conn);
            oleAdapter.Fill(tblResult);

            return tblResult;
        }

我的问题是,如何将结果集存储到模型中(例如,我不想返回 DataTable)。下面是我的模型类。

Class TransactionModel
{
    public int transID { get; set; }
    public int clientID { get; set; }
    public string clientName { get; set; }
    public double cashValue { get; set; }
    public double amout { get; set; }
    public DateTime transDate { get; set; }
    public string remarks { get; set; }
}
4

3 回答 3

2

您可以使用LINQ并执行以下操作:

var tranModel = from r in tblResult.Tables[0]
                select new TransactionModel 
                {
                   transId = r.Field<int>("transID"),
                   clientId = r.Field<int>("clientId"),
                   clientName = r.Field<string>("ClientName")
                }

请注意,由于您使用的.NET 2.0.LINQ 不能直接使用。你将不得不使用类似的东西LINQBridgehttp ://www.albahari.com/nutshell/linqbridge.aspx

另一种选择是遍历所有行,tblResult并有一个通用列表TransactionModel。例如:

 List<TransactionModel> tModels = new List<TransactionModel>();
 foreach (var row in tblResult.Tables[0].Rows) 
 {
     tModels.Add(new TransactionModel 
                 {
                   transId = row["TransId"],
                   clientId = row["ClientId"],     
                   clientName = row["clientName"]
                 });
 }
于 2013-03-20T10:25:59.803 回答
2

因为 LINQ 在 .NET 2 中不可用,所以您必须自己遍历这些项目并将它们转换为您的类型。像这样的东西:

DataTable transactions = getAllTransactions();
List<TransactionModel> model = new List<TransactionModel>();

foreach (DataRow transaction in transactions.Rows)
{
    TransactionModel tran = new TransactionModel
                            {
                                transId = transaction.Field<int>("transID"),
                                clientId = transaction.Field<int>("clientId"),
                                clientName = transaction.Field<string>("ClientName")
                                //etc...
                            };

    model.Add(tran);
}
于 2013-03-20T11:05:16.433 回答
1

执行以下操作:

List<TransactionModel> TransactionItems = tblResult.AsEnumerable().Select(r => 
new TransactionModel
    {
        transID  = r.Field<int>("TransactionID"),
        clientID = r.Field<int>("clientID"),
        and so on.....
    }).ToList();

return items;
于 2013-03-20T10:28:48.747 回答