0

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 Answers 2

2

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(); 

}
Sign up to request clarification or add additional context in comments.

3 Comments

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
Actuall i saw there is a property called entities and under that is items but its "NOT" public..
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 }
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.