?
Solved

LINQ return type for nested group query

Posted on 2016-08-07
6
Medium Priority
?
129 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
 
LVL 1

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 1

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 2000 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
 
LVL 1

Author Closing Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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