在 Linq 中将 IEnumerable 列表转换为分组列表

发布于 2024-09-17 05:45:46 字数 508 浏览 4 评论 0 原文

我有一个返回以下类型的函数:

 IEnumerable<IGrouping<String, ExportTransaction>>

在此函数中,我使用 linq 表达式创建以下类型的列表:

  IEnumerable<ExportTransaction>

Linq 代码如下所示:

 IEnumerable<ExportTransaction> transactions = ctx.ExportTransactions
                                                  .Where(x => x.Id != null);

如何将“事务”转换为以下所示类型的分组列表本页顶部。该函数通过“事务”执行各种操作,因此它必须在函数内保留为“IEnumerable”,但在返回时必须转换为分组列表。

I have a function that returns the following type:

 IEnumerable<IGrouping<String, ExportTransaction>>

In this function I create a list of the following type, using a linq expression:

  IEnumerable<ExportTransaction>

The Linq code looks like this:

 IEnumerable<ExportTransaction> transactions = ctx.ExportTransactions
                                                  .Where(x => x.Id != null);

How can I convert “transactions” to a grouped list of the type shown at the top of this page. The function does various things with the “transactions” so It must stay as “IEnumerable” inside the function but must be converted to the grouped list when returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

乖乖公主 2024-09-24 05:45:46

我假设交易有一个 name 属性,这就是您想要对交易进行分组的依据。如果情况并非如此,您所要做的就是更改 GroupBy 调用中的属性:

var transactions = ctx.ExportTranactions
                      .Where(x => x.Id != null)
                      .GroupBy(x => x.Name);

I'll assume that the Transactions have a name property and that's what you want to group the Transactions by. If that's not the case, all you have to do is change the property in the GroupBy call:

var transactions = ctx.ExportTranactions
                      .Where(x => x.Id != null)
                      .GroupBy(x => x.Name);
峩卟喜欢 2024-09-24 05:45:46

它真的需要分组吗?如果需要,您想按什么分组?分组?在我看来,您拥有数据集,并且您可能会尝试将其强制转换为您不一定需要的格式。

通常,要对数据集进行分组,您需要执行以下操作:

var grouping = ctx.ExportTransactions.Where(x => x.Id != null)
                                     .GroupBy(x => x.Description);

这将创建一个 IEnumerable>,将具有相同描述的所有交易分组,并假定每个交易都有描述。

如果您需要单个组中的所有记录,您始终可以执行以下操作:

    var grouping = ctx.ExportTransactions.Where(x => x.Id != null)
                                     .GroupBy(x => string.Empty);

这将为您提供所需的组键为空字符串的信息,但我强烈建议不要这样做,而是建议查看为什么当您似乎不想按任何内容进行分组时需要返回分组。

Does it really need to be grouped and if so what do you want to group it by? It sounds to me like you have your data set and you could be trying to force it into a format you don't necessarily need.

Typically to group a data set you'd do something like:

var grouping = ctx.ExportTransactions.Where(x => x.Id != null)
                                     .GroupBy(x => x.Description);

That would create an IEnumerable<IGrouping<string, ExportTransaction>> grouping all the transactions with an identical description, presuming that each transaction had description.

If you need all the records in a single group you can always do the following:

    var grouping = ctx.ExportTransactions.Where(x => x.Id != null)
                                     .GroupBy(x => string.Empty);

Which will give you what you need with the group key being an empty string, but I'd strongly advise against it and instead suggest looking at why you need to return a grouping when you don't seem to want to group by anything.

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