Solved

LINQ return type for nested group query

Posted on 2016-08-07
6
118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

705 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