检查型号是否有字段

时间:2014-03-11 04:56:03

标签: asp.net-mvc-4 c#-4.0

我的代码

 var bvcD = _db.BVCData.GetBVCDataByMetric(metric);
 // this one is simple "Select * from table"

从DB获取记录器列表。

我可以使用foreach循环遍历数据

foreach (var item in bvcD)
{
    // How can I check if "item" has column/key "report" ?
}

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

foreach (var item in bvcD)
{
    Type t = item.GetType();
    var props = t.GetProperties();
    foreach (var p in props)
    {
        string columnName = p.ToString();
        // Column name with type prepended
        // I get column name by doing columnName.Replace("Double ", "");
        // "Double" is the column dataType here
    }
}

这对于检索列名来说似乎是一种丑陋的方式。 如果您有任何其他方法可以告诉我。