如何使用列名作为变量而不是硬类型

时间:2015-02-09 22:44:46

标签: c# linq

我访问数据库的代码是..

using (MyDataContext target = DataContextFactory<MyDataContext>.GetDataContext())
{
    var table_orders = from tab in target.Orders
                       select tab;

    var cols = table_orders.First();

    var colNames = (from property in cols.GetType().GetProperties()
                    select property.Name).ToList();

    string[] fieldvalues;
    foreach (var row in table_orders)
    {
        var i = 0;
        foreach (var col in colNames)
        {
           var fieldvalues[i] = row.______.ToString(); <here I need to use 'col' variable not the hard typed property i.e., Order_Id, Order_Desc etc.
           i++;
        }
    }
}

我如何完成它?我试图读取没有硬类型列名称的表,并将每一行加载到字符串数组中。如果还有其他方式也让我知道。

0 个答案:

没有答案