views:

305

answers:

2

Hi everyone,

I am building my first few projects with ASP.NET MVC, and I have a list of items that come from 2 tables.

Using the nerd dinner example, this is my problem.

Lets say I want to have all the dinners, with all the rsvp information in a list on one page.

So it would go

dinnername1

rsvp1 rsvp2

dinnername2

rsvp1 rsvp2 rsvp3

Currently I'm using this linq to bring what I think is the correct data back

public IQueryable<Dinner> GetAllUserDinnersAndRSVPs(string userId)
    {
        return from dinner in db.Dinner
               from rsvp in db.RSVP
               where dinner.userId == userId
               where rsvp.userId == userId
               select dinner;
    }

This I think is bringing back the correct number of results, but because I am only bringing back the dinner type, the rsvp information doesn't show up in the collection.

So I think what I need to do is build a custom Model (class?) which will store all the info from the dinners table and the rsvp table, called something like DinnerTask.cs

This will basically just have a load of properties for each of the columns of both of the tables, and will mean that I can sort the entire list in c# before using a foreach to render out to the page.

I guess the defination of the class would be something like this.

public class DinnersAndRSVPsModel
{


        public int DinnerId { get; set; }
        public string DinnerName { get; set; }
        rest of fields for table 1......

        public int RSVPId { get; set; }
        public int RSVPInfo { get; set; }
        rest of fields for table 2.......
}

I'm asking this question because I think this seems like I am doing this wrong, and hopefully someone can point me in the right direction for working with data like this.

I would really appreciate any examples of this including the linq query, class defination etc... Bonus points if you could help me with any of the sorting as well, but I think I should be able to work that out.

Thank you for any help.

A: 

I would imagine that the object model would already be structured in such a way that a Dinner would have a list of RSVPs. I could be wrong ( I haven't looked at the NerdDinner example), but if that is indeed the case then all you need to do is specify the load options for your dinner to include the RSVPs.

It would look something like this:

public IQueryable<Dinner> GetAllUserDinnersAndRSVPs(string userId)
{
    DataLoadOptions options = new DataLoadOptions();
    options.LoadWith<Dinner>(d => d.RSVPs); //whatever property has the RSVPS
    db.LoadOptions = options;

    return from dinner in db.Dinner
           where dinner.userId == userId
           select dinner;
}
Joseph
This didn't seem to work, quick watching the list just becuase it gets passed into the view states now that it is only retrieving dinner items, and no sign of any rsvp data?
optician
A: 
optician