按列拆分查询结果的最佳实践

发布于 2024-07-29 23:51:06 字数 126 浏览 3 评论 0原文

我有一个查询,它一次性返回多个数据系列的数据。

我想通过数据系列 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-08-05 23:51:07

我建议按 DataSeriesId 对结果进行分组。

var groupedResult = QueryDatabase().GroupBy(item => item.DataSeriesId);

现在您可以按如下方式访问分组数据。 该示例将仅打印包含所有项目的所有组。

 foreach(var group in groupedResult)
 {
     Console.WriteLine("Group: " + group.Key);

     foreach(var item in group)
     {
         Console.WriteLine("  Item: " + item);
     }
 }

或者您可以将数据库查询结果分组到列表的列表中。

IList<IList<DataItem>> = QueryDatabase()
    .GroupBy(item => item.DataSeriesId)
    .Select(group => group.ToList())
    .ToList();

或者,您可以构建一个从 DataSeriesId 到数据库查询结果中的数据项列表的字典。

IDictionary<Int32, IList<DataItem>> = QueryDatabase()
    .GroupBy(item => item.DataSeriesId)
    .ToDictionary(group => group.DataSeriesId, group => group.ToList());

或者,如果您以后不想更改字典,请使用Enumerable.ToLookUp()

更新

刚刚注意到问题中的“最佳实践”和“DataReader”标签。 LINQ 易于编写且易于正确使用,但它可能不是最快的解决方案。 因此,根据您的要求,使用 LINQ 可能是一个好的选择,也可能是一个坏的选择。 如果性能(还)不是问题的话,我更喜欢 LINQ。 否则,我会考虑在读取数据时构建从 DataSeriesId 到数据项列表的字典。

IDictionary<Int32, IList<DataItem>> result =
    new Dictionary<Int32, IList<DataItem>>();

while (dataSource.DataAvailiable)
{
    DataItem item = dataSource.ReadItem();

    IList<DataItem> items;
    if (!result.TryGetValue(item.DataSeriesId))
    {
        items = new List<DataItem>();

        result.Add(item.DataSeriesId, items);
    }

    items.Add(item);
}

I suggest grouping the result by the DataSeriesId.

var groupedResult = QueryDatabase().GroupBy(item => item.DataSeriesId);

Now you can access the grouped data as follows. The example will just print all groups with all items.

 foreach(var group in groupedResult)
 {
     Console.WriteLine("Group: " + group.Key);

     foreach(var item in group)
     {
         Console.WriteLine("  Item: " + item);
     }
 }

Or you can group the database query result into a list of lists.

IList<IList<DataItem>> = QueryDatabase()
    .GroupBy(item => item.DataSeriesId)
    .Select(group => group.ToList())
    .ToList();

Or you can build a dictionary from DataSeriesId to a list of data items from the database query result.

IDictionary<Int32, IList<DataItem>> = QueryDatabase()
    .GroupBy(item => item.DataSeriesId)
    .ToDictionary(group => group.DataSeriesId, group => group.ToList());

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.

IDictionary<Int32, IList<DataItem>> result =
    new Dictionary<Int32, IList<DataItem>>();

while (dataSource.DataAvailiable)
{
    DataItem item = dataSource.ReadItem();

    IList<DataItem> items;
    if (!result.TryGetValue(item.DataSeriesId))
    {
        items = new List<DataItem>();

        result.Add(item.DataSeriesId, items);
    }

    items.Add(item);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文