?
Solved

Struggling with linq

Posted on 2014-03-30
6
Medium Priority
?
257 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 1000 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 64

Accepted Solution

by:
Fernando Soto earned 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

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 64

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

719 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