Solved

Struggling with linq

Posted on 2014-03-30
6
251 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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