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

查看:74
本文介绍了Linq的挑战:将这段代码从方法链转换为标准的Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挑战在于从方法链到标准linq转换一组充满分组依据的代码.

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

要在此处完全理解该主题,您可以阅读原始问题(包括类定义,示例数据等):

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

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

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

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公式,但是出了点问题.

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

发生的事情与此相似.

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中的结果

https://dotnetfiddle.net/8mF1qI

推荐答案

此挑战帮助我了解了什么确切地返回了Linq Group By(以及Group By的Linq语法的复杂程度).

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

LinqPad清楚地显示了 Group By 返回GroupsList. Group是一个非常简单的类,它具有个属性:一个 Key

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

此答案指出,根据IGrouping(IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable)的定义,访问子组内容的方法是遍历元素(一个foreach,另一个group,一个select,ecc).

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).

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

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

这是小提琴上的源代码

但是让我们继续尝试其他解决方案:

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

当我们执行分组依据时,在 SQL 中通常要做的是列出所有列,但已分组的列. 与Linq不同..它仍然返回所有列.

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.

在此示例中,我们从具有3个列" {IDMacroTab,IDTab,IDSlot}的数据集开始.我们对第一列进行了分组,但是除非明确告诉他,否则Linq会返回整个数据集.

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..

这篇关于Linq的挑战:将这段代码从方法链转换为标准的Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆