views:

106

answers:

2

Hi there,

Been succcesfully adding records using Linq2Sql with the linq2sql genereated classes, works great..

But i need to now select the records, i can't seem to figure this out

This is how i am adding records - reservation is a generated linq2sql class

            TestDataContext db = new TestDataContext();

            db.Reservations.InsertOnSubmit(reservation);
            db.SubmitChanges();

So i wanted to pass in a reservation number (which is a field) a retrieve a populated Reservation class

Can anyone help?

Thanks

+2  A: 

If you're using a strongly typed datacontext object you should be able to do something like this:

public Reservation GetReservation(int id) {

return db.Reservations.Where(r =>   r.ReservationId == id ).SingleOrDefault(); 

}
Robban
Thanks! yes that worked but my reservation class has linked object i.e. I have a class called "Calls" and a Class called "Dropped" - both are actualy separate tables, I see properties of Count which is 1 on Calls and 3 on Dropped.. Do i need to explicitly create a calls object or something to view the details? Thanks again
mark smith
Actuall i saw there is a property called entities and under that is items but its "NOT" public..
mark smith
No, as a matter of fact your Reservation object should have a propertie named Calls which is actually a list of all Calls linked to this Reservation. To get the Calls you could simply loop through each Call like this: foreach(Call call in Reservation.Calls) { //Do something with the call }
Robban
thank you for a great explanation.
mark smith
A: 

You can do something like:

Reservation reservation = 
    db.Reservations.Where(r => r.id == reservationId).Single();

or

// Use this if you're not positive the single record exists
Reservation reservation = 
    db.Reservations.Where(r => r.id == reservationId).SingleOrDefault();

You can now work with reservation and then db.SumbitChanges() will save any modifications to the object back to the database.

Justin Niessner