Avatar of David Huisman
David Huisman

asked on 

LINQ return type for nested group query

I have the following query working in my code and now want to move into a separate class as a method that I call with List<Logrecord>, but having trouble determining what TYPE I need to return from the method.

recsByDate is a List<LogRecord> where records have Date, Time, EventType fields etc.

var vehicleGroupedByDate =  from daterecs in recsByDate
                                        where daterecs.Event == "Detection"
                                        orderby daterecs.Date
                                        group daterecs by daterecs.Date into vrecs
                                        from vrecs2 in
                                            (
                                               from daterecs in vrecs
                                               orderby daterecs.Time
                                               group daterecs by daterecs.Vehicle
                                            )
                                        group vrecs2 by vrecs.Key;
LINQ QueryVisual Basic Classic.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
David Huisman
Avatar of louisfr
louisfr

You can find what the type of vehicleGroupedByDate is by hovering the mouse over the 'var' keyword.
Assuming that Date's type is DateTime and Vehicle's type is String, it should be:
 IGrouping<DateTime, IGrouping<string, LogRecord>>
Avatar of David Huisman

ASKER

I tried using the suggested return type and get error

Error      CS0266      Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<System.DateTime, System.Linq.IGrouping<string, BodyGuardDesktop.BodyGuardUserFriendlyLogRecord>>>' to 'System.Linq.IGrouping<System.DateTime, System.Linq.IGrouping<string, System.Collections.Generic.List<BodyGuardDesktop.BodyGuardUserFriendlyLogRecord>>>'. An explicit conversion exists (are you missing a cast?)


public IGrouping<DateTime, IGrouping<string, List<BodyGuardUserFriendlyLogRecord>>> GetVehicleDetections(List<BodyGuardUserFriendlyLogRecord> recsByDate)
        {
            var vehicleQuery = from daterecs in recsByDate
                                       where daterecs.Event == "Detection"
                                       orderby daterecs.Date
                                       group daterecs by daterecs.Date into vrecs
                                       from vrecs2 in
                                           (
                                              from daterecs in vrecs
                                              orderby daterecs.Time
                                              group daterecs by daterecs.Vehicle
                                           )
                                       group vrecs2 by vrecs.Key;

            return vehicleQuery;
        }
Avatar of louisfr
louisfr

The error message gave you the type you were looking for:
IEnumerable<IGrouping<DateTime, IGrouping<string, List<BodyGuardUserFriendlyLogRecord>>>>

When I tried it, I checked only the items type, so the IEnumerable was missing.
Avatar of David Huisman

ASKER

Changed to type as you suggest and get error

Severity      Code      Description      Project      File      Line      Suppression State
Error      CS0266      Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<System.DateTime, System.Linq.IGrouping<string, BodyGuardDesktop.BodyGuardUserFriendlyLogRecord>>>' to 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<System.DateTime, System.Linq.IGrouping<string, System.Collections.Generic.List<BodyGuardDesktop.BodyGuardUserFriendlyLogRecord>>>>'. An explicit conversion exists (are you missing a cast?)      


public IEnumerable<IGrouping<DateTime, IGrouping<string, List<BodyGuardUserFriendlyLogRecord>>>>
        GetVehicleDetections(List<BodyGuardUserFriendlyLogRecord> recsByDate)
        {
            var vehicleQuery = from daterecs in recsByDate
                               where daterecs.Event == "Detection"
                               orderby daterecs.Date
                               group daterecs by daterecs.Date into vrecs
                               from vrecs2 in
                                   (
                                      from daterecs in vrecs
                                      orderby daterecs.Time
                                      group daterecs by daterecs.Vehicle
                                   )
                               group vrecs2 by vrecs.Key;
            return vehicleQuery;
        }
ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of David Huisman
David Huisman

ASKER

Thanks for your help
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo