Solved

Struggling with linq

Posted on 2014-03-30
6
245 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 74

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 62

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 62

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 62

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now