0

I'm having a few problems using a foreach loop with LINQ, this is the code I have so far what I'm trying to do is get a list of customers associated with a particular booking, any help would be appreciated =]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;   


namespace BookingCustomers
{
    public partial class BookingGuests : System.Web.UI.Page
    {
        private HotelConferenceEntities datacontext = new HotelConferenceEntities();



        private void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                try
                {

                    int id = int.Parse(BookID.Text.ToString());
                    tblBooking booking = datacontext.tblBookings.SingleOrDefault(x => x.BookingID == id);

                    tblVenue venue = datacontext.tblVenues.SingleOrDefault(x => x.VenueID == booking.Venue);


                    List<tblCustomer> customers = new List<tblCustomer>();
                    List<tblBookingGuest> guests = booking.tblBookingGuests.ToList();



                    foreach (list<tblBookingGuest> in tblBookingGuest)
                    {



                    }
}
David
  • 15,894
  • 22
  • 55
  • 66
Captain_Custard
  • 1,308
  • 6
  • 21
  • 35
  • Here is the Related Thread in this Forum http://stackoverflow.com/questions/7816781/tolist-foreach-in-linq – SaravanaKumar May 30 '13 at 09:45
  • `having a few problems`. okay, let me turn the telepathist mode on and guess which – Artur Udod May 30 '13 at 09:46
  • The question is totally unclear. Are you trying to retrieve all customers from guests? How can you retrieve from a selected guests item the customers? Has it functions/properties? – Francesco De Lisi May 30 '13 at 09:48
  • _get a list of customers associated with a particular booking_ This is not about how to use `foreach`! This is about how to query. For that we need to see your class model. – Gert Arnold May 30 '13 at 10:10

4 Answers4

2

You are missing the loop variable declaration, and the declared type was wrong - oh, and you were using the wrong sequence. I think this is what you want:

foreach (var guest in booking.tblBookingGuests)
{
    // Do something with guest
}

Note that your line of code

List<tblBookingGuest> guests = booking.tblBookingGuests.ToList();

is superfluous. It will make a copy of the entire sequence of booking guests.

You should just use booking.tblBookingGuests directly in the foreach unless you are going to modify the list itself rather than the items in it. (If you do that, it won't change the original, of course.)

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • I just tried this Matthew and I get an error from Visual Studio that says BookingCustomers.tblBookingGuest is a type but is used like a 'variable – Captain_Custard May 30 '13 at 09:43
  • @ReeceCottam I think I might have ninja-edited my reply. Does it work now? – Matthew Watson May 30 '13 at 09:45
  • 1
    Superfluous for some, more readable and maintainable for others, as for speed, the compilers optimizer will most likely produce identical code for either. – Paul Zahra May 30 '13 at 10:00
  • 2
    @PaulZahra You don't seem to be understanding what I'm saying. The `.ToList()` will be making an entire copy of the sequence, and *that* is what is superfluous. Can't you see the `ToList()` at the end there? You don't need to make an entire copy of a sequence in a `List<>` before you enumerate over it! – Matthew Watson May 30 '13 at 10:17
  • of course... my bad... bit busy buying a house today... mind elsewhere... was thinking only of the foreach statement to be honest – Paul Zahra May 30 '13 at 12:49
1

Surely what you want is:

foreach (tblBookingGuest guest in guests)
{
  ...
}
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
1

I guess you want to access to all the tblBookingGuest in the booking variable.

foreach (tblBookingGuest guest in guests)
{
 //something
}

Please remember that you cannot directly modify a member of the guests list into the foreach loop.

Hope it could help.

Gauthier G. Letellier
  • 3,305
  • 1
  • 16
  • 12
1

How about pure linq :

    booking.tblBookingGuests.ToList().ForEach(a =>
    {
        // Do your stuff
    });
tariq
  • 2,193
  • 15
  • 26