简体   繁体   English

如何分组然后在LINQ中选择元素到新类(最好是C#)

[英]How to group then select elements into new class in LINQ (C# preferably)

Hi I am trying to get my head around grouping, and then building my own class in the result. 嗨,我试图让我的头围绕分组,然后在结果中构建我自己的类。 I know the result of a group by is an IGrouping collection but can I access the rows as they are being built to add a couple of flags to them with a custom class? 我知道group by的结果是一个IGrouping集合,但是我可以在构建它们的时候访问这些行,以便使用自定义类向它们添加几个标志吗?

I have a class called FlightTimes with some data, but I'd like to append some data to the rows, like a FlagRedEye. 我有一个名为FlightTimes的类,有一些数据,但我想在行中附加一些数据,比如FlagRedEye。 So I created a class called FlightTimeResult with the original FlightTime class data plus the flag. 所以我用原始的FlightTime类数据和标志创建了一个名为FlightTimeResult的类。

Can I do this? 我可以这样做吗? I can't seem to figure out how to get it to work. 我似乎无法弄清楚如何让它工作。 I like to use strong types until I understand what is going on. 我喜欢使用强类型,直到我理解发生了什么。 I had to change a few things to protect my client so I apologize for any syntax errors. 我不得不改变一些事情来保护我的客户端,所以我为任何语法错误道歉。

IGrouping<string, FlightTimeResult> FlightTimes =
               ( from flighttimes in schedules.FlightTimes
                 group flighttimes by flighttimes.FlightType.ToString()
                     into groupedFlights
                 select new FlightTimeResult( )
                 {
                     FlightTimeData = FlightTime,   // Original class data
                     FlagRedEye = (FlightTime.departureTime.Hour >= 0 &&
                                  FlightTime.departureTime.Hour < 6) // Extra flag
                 } )

The goal is to have a collection of FlightTimesResult (FlightTime + extra flag) grouped by FlightType. 目标是拥有一个按FlightType分组的FlightTimesResult(FlightTime +额外标志)的集合。 Not sure how to access the individual FlightTime rows in the query 'select new FlightTimeResult()' 不确定如何访问查询中的各个FlightTime行'select new FlightTimeResult()'

Do i need to use a nested query on the groupedFlights? 我是否需要在groupedFlights上使用嵌套查询?

Thank you very much. 非常感谢你。

It is easiest achieved by calling Linq functions explicitly in following way: 通过以下方式显式调用Linq函数是最简单的方法:

IQueryable<IGrouping<string, FlightTimeResult>> query 
         = schedules.FlightTimes.GroupBy(
               ft => ft.FlightType.ToString(), // key 
               ft => new FlightTimeResult() { // your constructed objects for key
                    FlightTimeData = ft,
                    FlagRedEye = (ft.departureTime.Hour >= 0 && ft.departureTime.Hour < 6)
                    }
               );

The two-argument GroupBy operator function takes two lambdas as arguments - one for extracting keys, second for extracting values for it. 两参数GroupBy运算符函数将两个lambdas作为参数 - 一个用于提取键,第二个用于为其提取值。

Also keep in mind that group by operation (be it group itm by key construction or GroupBy call) returns a collection of IGrouping<,> s - not a single one. 还要记住, 操作分组group itm by key构造或GroupBy调用将它group itm by key )会返回IGrouping<,> s的集合 - 而不是单个集合。

Thus it will be IEnumerable<IGrouping<,>> or IQueryable<IGrouping<,>> . 因此它将是IEnumerable<IGrouping<,>>IQueryable<IGrouping<,>>

I think you're on the right track. 我认为你走在正确的轨道上。 Instead of grouping FlightTimes by FlightType, try building FlightTimeResults and grouping those by FlightType instead: 不是通过FlightType对FlightTimes进行分组,而是尝试构建FlightTimeResults并将其分类为FlightType:

var results = 
    from ft in schedules.FlightTimes
    group new FlightTimeResult
        {
            FlightTimeData = ft,
            FlagRedeye = ft.DepartureTime.Hour >= 0 && ft.DepartureTime.Hour < 6
        }
    by ft.FlightType.ToString()
    into groupedFlights
    select groupedFlights;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM