tags:

views:

32

answers:

2

Hi,

I have 2 tables here: Auction and Article.

1 Auction has 1 Article. 1 Aricle has x Auctions.

Now I load a list of auctions:

            using (APlattformDatabaseDataContext dc = new APlattformDatabaseDataContext())
        {
            List<Auktion> auctionlist = (from a in dc.Auktion
                                         where a.KäuferID == null
                                         select a).ToList();
            return auctionlist;
        }

But when I want to "txtBla.Text = auction[0].Article.Text;" it isn't loaded. The question isn't why (it is logic that it isn't loaded allready and can't be loaded because the DC is closed), but how can I solve this without letting the DC open?

+2  A: 

You can do the following:

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Auktion>(a => a.Article);
dc.LoadOptions = options;
Lazarus
Very easy, tahnks.Must I do this within every new opened dc?
Kovu
Because you are instantiating the context in this way, yes, you'll have to set the DataLoadOptions each time you instantiate the context. you might want to abstract the instantiation to a helper function that includes the DLO so the code exists only in 1 place.
Lazarus
A: 

If you want to eager load the associations like that you should use the DataContext.LoadOptions property like so...

using (APlattformDatabaseDataContext dc = new APlattformDatabaseDataContext())
{
    var dlo = new DataLoadOptions();
    options.LoadWith<Auktion>(o => o.Article);
    dc.LoadOptions = dlo;

    List<Auktion> auctionlist = (from a in dc.Auktion
                                 where a.KäuferID == null
                                 select a).ToList();
    return auctionlist;
}

That way your articles will be loaded when your Auktion objects are retrieved from the database.

Jason Punyon