使用 linq 连接两个表,并填充它们的字典
- 作者: 蒜蓉专家
- 来源: 51数据库
- 2022-12-15
问题描述
我一直在寻找如何连接两个表(Data 和 DataValues,一对多)并填充类型为 的字典.
I've been searching how to join two tables (Data and DataValues, one to many) and fill a dictionary of type .
Data(s) 的记录可能是数千(例如 500,000 或更多),并且每个 Data 可能有 10 到 20 个 DataValues,这使得它的查询量更大,因此性能在这里非常重要.
The records of Data(s) might be thousands (e.g. 500,000 or more) and each Data may have 10 to 20 DataValues which makes it a much heavier query, so the performance is really important here.
这是我写的代码:
// 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 视图.有什么建议吗?
But this approach has a significant performance issue and takes a long time to execute. Not sure if I need to use SQL Views. any suggestions?
推荐答案
您查询的次数太多了.您可以在一个查询中完成此操作.
You're querying way too many times. You can do this in one query.
var dict = (from d in dt.Datas
join dV in dt.DataValues on d.DataID equals dv.DataID
where SensorIDs.Contains(dv.SensorID)
select new { d, dV }).ToDictionary(o => o.d, o => o.dV.ToList());
在您的 foreach 循环中,您正在获取所有 Data 并且对于它们中的每一个,您都在做同样的事情.
In your foreach loop, you are fetching all Data and for each of them, you are doing the same thing.
现在还不是很清楚,但我认为您只想加入 SensorIDs 数组中的 DataValues.在这种情况下:
Now that wasn't very clear, but I think you want to join only the DataValues that are in the SensorIDs array. In this case:
var dict = (from d in dt.Datas
let dV = (from dataValue in dt.DataValues
where SensorIDs.Contains(dataValue.SensorID) &&
dataValue.DataID = d.DataID
select dataValue)
select new { d, dV }).ToDictionary(o => o.d, o => o.dV.ToList());
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
