我一直在搜索如何连接两个表(Data 和 DataValues,一对多)并填充类型的字典。
Data(s) 的记录可能有数千条(例如 500,000 条或更多),每个 Data 可能有 10 到 20 个 DataValue,这使得查询更加繁重,因此性能在这里非常重要。
这是我写的代码:
// Passed via the arguments, for example, sensorIDs would contain:
int[] sensorIDs = { 0, 1, 2, 3, 4, 5, 6, 17, 18 };
Dictionary<Data, List<DataValue>> dict = new Dictionary<Data, List<DataValue>>();
foreach (Data Data in dt.Datas)
{
var dValues = from d in dt.Datas
join dV in dt.DataValues on d.DataID equals dV.DataID
where (SensorIDs.Contains(dV.SensorID))
select dV;
dict.Add(Data, dValues.ToList<DataValue>());
}
但是这种方法存在严重的性能问题,并且需要很长时间才能执行。不确定我是否需要使用 SQL 视图。有什么建议么?