简体   繁体   中英

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?

I have a class called FlightTimes with some data, but I'd like to append some data to the rows, like a FlagRedEye. So I created a class called FlightTimeResult with the original FlightTime class data plus the flag.

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. Not sure how to access the individual FlightTime rows in the query 'select new FlightTimeResult()'

Do i need to use a nested query on the groupedFlights?

Thank you very much.

It is easiest achieved by calling Linq functions explicitly in following way:

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.

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.

Thus it will be IEnumerable<IGrouping<,>> or 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:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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