Solved

LINQ return type for nested group query

Posted on 2016-08-07
6
59 Views
Last Modified: 2016-08-21
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;
0
Comment
Question by:David Huisman
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:louisfr
ID: 41746709
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>>
0
 

Author Comment

by:David Huisman
ID: 41746829
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;
        }
0
 
LVL 11

Expert Comment

by:louisfr
ID: 41746835
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:David Huisman
ID: 41747000
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;
        }
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 41747105
My fault. Should copy/paste instead of retyping.
Sorry.
The error message gives you the right type.

System.Collections.Generic.IEnumerable<System.Linq.IGrouping<System.DateTime, System.Linq.IGrouping<string, BodyGuardDesktop.BodyGuardUserFriendlyLogRecord>>>
0
 

Author Closing Comment

by:David Huisman
ID: 41764746
Thanks for your help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now