Solved

Struggling with linq

Posted on 2014-03-30
6
254 Views
Last Modified: 2016-02-10
I haven't got up to speed with linq and need quick fix please.
I have a flat table of bookings i.e. people at certain locations on certain dates.
I would like to use linq to convert into objects that are shown in the image: each instance of the BookingViewModel represents a date, and it has a date, a location and a collection of bookings.
How do I use linq to ?group the flat table by date and location, and then use the result to populate the collection property. All I seem to have access to is the "Key".

I've tried:
 var group =
                    bookingRepository.GetAll().GroupBy(b => b.fkDateTimeObjectId, b=>b.fkLocationId);
Linq-question.png
0
Comment
Question by:quentinA
[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
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39965494
I think query syntax would be easier to swallow in this particular case, so...

var query = from booking in bookingRepository.GetAll()
            group booking by new { booking.Date, booking.Location } into g
            select new BookingViewModel()            
            {
            	Date = g.Key.Date,
            	Location = g.Key.Location,
            	Bookings = g.ToList(),
            };

Open in new window


You create a new anonymous object with all of the properties that you will be grouping on. Then simply select a new instance of your view model class, and assign each property. The Key will have the two properties that you grouped on, so you can use that to assign those two particular fields.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 39965675
Hi quentinA;

If I understand the question correctly this should give you what you want.

var group = bookingRepository.GetAll()
                             .GroupBy(b => new { b.Date, b.Location})
                             .Select( g => new BookingViewModel() 
                             {
                             	    Date = g.Key.Date,
            	                    Location = g.Key.Location,
            	                    Bookings = g.Select(p => p.BookedPerson).ToList()
                             });

Open in new window

0
 

Author Closing Comment

by:quentinA
ID: 39966051
Thank you both.
Can you recommend a linq book/ training (or should I post this as another question?)
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39966599
Hi ;

I have used LinqPad which is a free software package for learning and testing Linq queries. The software is free to download and use. They also have a paid option which add autocompletion but only for C# NOT VB .Net.

The software contains many working examples in its library and the website also contains helpful information.

LinqPad

LINQPad standard edition is free to download and use. Autocompletion is an optional extra.
0
 

Author Comment

by:quentinA
ID: 39967128
Thanks again! That's great.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39967158
Not a problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

632 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