1

我有一个表,Parameters其中包含IdName、等列。该数据库还包含多个“系列数据”表,这些表有几个,可能是数百个列,包括一个列。我不拥有 db 架构。所以我无法改变这一点。TableNameColumnNameTimestamp

用户必须通过 GUI提供NameParameteraStartTime和 an的 。EndTime业务逻辑必须返回一系列数据。

如何获取名称为param.TableNameand的列和表中的数据param.ColumnName

我目前正在使用这样的 switch 语句:

public List<double> GetData
                    (string paramName, DateTime startTime, DateTime endTime)
{
    using (var context = new MyEntities())
    {
        var param =
            (from p in context.Parameters where p.Name == paramName select p)
            .FirstOrDefault();

        switch (param.TableName)
        {
             case "Table1":
                 return GetTable1Data(columnName, startTime, endTime);
                 break;
             case "Table2":
                 return GetTable2Data(columnName, startTime, endTime);
                 break;
             default:
                 throw new Exception();
        }
    }
}

private List<double> GetTable1Data
   (MyEntities context, string columnName, DateTime startTime, DateTime endTime) {

    List<double> data = new List<double>();

    switch (columnName)
    {
        case "Parameter1":
            (from d in context.Table1 where d.Timestamp >= startTime && d.Timestamo <= endTime
            order by d.Timestamp select d).ForEach(x => data.Add(d.Parameter1));
            break;
        case "Parameter2":
            (from d in context.Table1 where d.Timestamp >= startTime && d.Timestamo <= endTime
            order by d.Timestamp select d).ForEach(x => data.Add(d.Parameter2));
            break;
       case "Parameter3":
           (from d in context.Table1 where d.Timestamp >= startTime && d.Timestamo <= endTime
           order by d.Timestamp select d).ForEach(x => data.Add(d.Parameter3));
           break;
       default:
           throw new Exception("");
    }

    return data;
}

private List<double> GetTable2Data(MyEntities context, string columnName, DateTime startTime, DateTime endTime)
{
    List<double> data = new List<double>();

    switch (columnName)
    {
        case "Parameter1":
            (from d in context.Table2 where d.Timestamp >= startTime && d.Timestamo <= endTime
            order by d.Timestamp select d).ForEach(x => data.Add(d.Parameter1));
            break;
        case "Parameter2":
            (from d in context.Table2 where d.Timestamp >= startTime && d.Timestamo <= endTime
            order by d.Timestamp select d).ForEach(x => data.Add(d.Parameter2));
            break;
        case "Parameter3":
            (from d in context.Table2 where d.Timestamp >= startTime && d.Timestamo <= endTime
            order by d.Timestamp select d).ForEach(x => data.Add(d.Parameter3));
            break;
        default:
             throw new Exception("");
    }

    return data;

}

这种重复似乎非常错误。

有没有更好的办法?

4

1 回答 1

1

您可以使用字符串形式的信息构建查询,然后像这样执行它:

return db.ExecuteStoreQuery<double>(
    "SELECT {0} FROM {1} WHERE [Timestamp] BETWEEN {2} AND {3}",
    columnName,
    tableName,
    startTime,
    endTime).ToList();

皮特

于 2012-06-19T20:51:44.143 回答