Solved

LINQ return type for nested group query

Posted on 2016-08-07
6
112 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

738 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