LINQ挑战:将此代码从方法链转换为标准LINQ[英] Linq challenge: converting this piece of code from method chain to standard Linq

本文是小编为大家收集整理的关于LINQ挑战:将此代码从方法链转换为标准LINQ的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

挑战是要从方法链转换为标准linq一条包含组的代码.

上下文

要充分理解这里的主题,您可以阅读原始问题(具有类定义,示例数据等): linq:从扁平列表中重建层次数据

感谢@Akash Kava,我找到了解决问题的解决方案.

链方法公式

var macroTabs = flattenedList
        .GroupBy(x => x.IDMacroTab)
        .Select((x) => new MacroTab
        {
            IDMacroTab = x.Key,
            Tabs = x.GroupBy(t => t.IDTab)
                    .Select(tx => new Tab {
                        IDTab = tx.Key,
                        Slots = tx.Select(s => new Slot {
                           IDSlot = s.IDSlot
                     }).ToList()
            }).ToList()
        }).ToList();

但是,为了了解知识,我尝试将方法链转换为标准的LINQ公式,但有些不对劲.

发生的事情与此相似.

我尝试将其转换为Linq标准语法

var antiflatten = flattenedList
    .GroupBy(x => x.IDMacroTab)
    .Select(grouping => new MacroTab
    {
        IDMacroTab = grouping.Key,
        Tabs = (from t in grouping
                group grouping by t.IDTab
                into group_tx
                select new Tab
                {
                    IDTab = group_tx.Key,
                    Slots = (from s in group_tx
                    from s1 in s    
                    select new Slot
                    {
                        IDSlot = s1.IDSlot
                    }).ToList()
                }).ToList()
    });

LINQPAD中的结果

在此处输入图像说明

NetFiddle上的类和示例数据:

https://do.netfiddle.net/8mf1qi

推荐答案

这个挑战帮助我了解了确切返回LINQ组的内容(以及prolix是linq语法与groups by by a linq语法).

.

as LinqPad清楚地显示了返回Groups的List组. Group是一个非常简单的类,只有一个属性:a 键

在此处输入图像说明

AS 这个答案从igrouping的定义(IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable)中指出, 唯一访问子组内容的方法是通过元素迭代(foreach,另一组,选择,ECC).

此处显示了方法链的LINQ语法公式.

在此处输入图像说明

,这是小提琴上的源代码

,但让我们继续尝试查看另一个解决方案:

我们通常在 sql 上做组时的是列出所有列,但已经分组. linq不同.它仍然返回所有列.

在此示例中,我们从带有3列'{idmacrotab,idtab,idslot}的数据集开始.我们分组为第一列,但是Linq会返回整个数据集,除非我们明确告诉他.

在此处输入图像说明

本文地址:https://www.itbaoku.cn/post/1556926.html

问题描述

The challenge is about converting from method chain to standard linq a piece of code full of group by.

The context

To fully understand the topic here you can read the original question (with class definitions, sample data and so on): Linq: rebuild hierarchical data from the flattened list

Thanks to @Akash Kava, I've found the solution to my problem.

Chain method formulation

var macroTabs = flattenedList
        .GroupBy(x => x.IDMacroTab)
        .Select((x) => new MacroTab
        {
            IDMacroTab = x.Key,
            Tabs = x.GroupBy(t => t.IDTab)
                    .Select(tx => new Tab {
                        IDTab = tx.Key,
                        Slots = tx.Select(s => new Slot {
                           IDSlot = s.IDSlot
                     }).ToList()
            }).ToList()
        }).ToList();

But, for sake of knowledge, I've tried to convert the method chain to the standard Linq formulation but something is wrong.

What happens is similar to this..

My attempt to convert it to Linq standard syntax

var antiflatten = flattenedList
    .GroupBy(x => x.IDMacroTab)
    .Select(grouping => new MacroTab
    {
        IDMacroTab = grouping.Key,
        Tabs = (from t in grouping
                group grouping by t.IDTab
                into group_tx
                select new Tab
                {
                    IDTab = group_tx.Key,
                    Slots = (from s in group_tx
                    from s1 in s    
                    select new Slot
                    {
                        IDSlot = s1.IDSlot
                    }).ToList()
                }).ToList()
    });

The result in LinqPad

enter image description here

The classes and the sample data on NetFiddle:

https://dotnetfiddle.net/8mF1qI

推荐答案

This challenge helped me to understand what exactly returns a Linq Group By (and how prolix is the Linq syntax with Group By).

As LinqPad clearly shows a Group By returns a List of Groups. Group is a very simple class which has just one property: a Key

enter image description here

As this answer states, from definition of IGrouping (IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable) the only way to access to the content of the subgroups is to iterate through elements (a foreach, another group by, a select, ecc).

Here is shown the Linq syntax formulation of the method chain.

enter image description here

And here is the source code on Fiddle

But let's go on trying to see another solution:

What we usually do in SQL when we do a Group By is to list all the columns but the one which have been grouped. With Linq is different.. it still returns ALL the columns.

In this example we started with a dataset with 3 'columns' {IDMacroTab, IDTab, IDSlot}. We grouped for the first column, but Linq would return the whole dataset, unless we explicitly tell him..

enter image description here

相关标签/搜索