我有一个表,Parameters
其中包含Id
、Name
、等列。该数据库还包含多个“系列数据”表,这些表有几个,可能是数百个列,包括一个列。我不拥有 db 架构。所以我无法改变这一点。TableName
ColumnName
Timestamp
用户必须通过 GUI提供Name
、Parameter
aStartTime
和 an的 。EndTime
业务逻辑必须返回一系列数据。
如何获取名称为param.TableName
and的列和表中的数据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;
}
这种重复似乎非常错误。
有没有更好的办法?