Solved

Struggling with linq

Posted on 2014-03-30
6
249 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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