按列拆分查询结果的最佳实践
我有一个查询,它一次性返回多个数据系列的数据。
我想通过数据系列 id 将此数据拆分为数据系列 id。 查询不能更改。
假设数据按系列 id 排序并不安全,那么最好的方法是什么? LINQ?
I have a query which returns data for a number of dataseries in one lump.
I would like to split this data up into the data series id, by the dataseries id. The query cannot be changed.
Its not safe to assume that the data is ordered by the series id, so what would be the best way to do this? LINQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议按
DataSeriesId
对结果进行分组。现在您可以按如下方式访问分组数据。 该示例将仅打印包含所有项目的所有组。
或者您可以将数据库查询结果分组到列表的列表中。
或者,您可以构建一个从
DataSeriesId
到数据库查询结果中的数据项列表的字典。或者,如果您以后不想更改字典,请使用
Enumerable.ToLookUp()
。更新
刚刚注意到问题中的“最佳实践”和“DataReader”标签。 LINQ 易于编写且易于正确使用,但它可能不是最快的解决方案。 因此,根据您的要求,使用 LINQ 可能是一个好的选择,也可能是一个坏的选择。 如果性能(还)不是问题的话,我更喜欢 LINQ。 否则,我会考虑在读取数据时构建从 DataSeriesId 到数据项列表的字典。
I suggest grouping the result by the
DataSeriesId
.Now you can access the grouped data as follows. The example will just print all groups with all items.
Or you can group the database query result into a list of lists.
Or you can build a dictionary from
DataSeriesId
to a list of data items from the database query result.Or use
Enumerable.ToLookUp()
if you don't want to alter the dictionary later.UPDATE
Just noticed the "Best practice" in the question an the "DataReader" tag. Well LINQ is easy to write and easy to get right but it is probably not the fastest solution. So depending on your requirements using LINQ might be a good or bad choice. I would prefer LINQ if performance is not (yet) an issue. Else I would consider building a dictionary from
DataSeriesId
to a list of data items while reading the data.