开发者

Linq: How to load a second table directly?

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())
        {
            Lis开发者_开发百科t<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?


You can do the following:

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Auktion>(a => a.Article);
dc.LoadOptions = options;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜