Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq:如何直接加载第二个表?_Linq - Fatal编程技术网

Linq:如何直接加载第二个表?

Linq:如何直接加载第二个表?,linq,Linq,我这里有两张桌子: 拍卖和物品 一次拍卖有一件物品。 1.Aricle有x次拍卖 现在,我加载拍卖列表: using (APlattformDatabaseDataContext dc = new APlattformDatabaseDataContext()) { List<Auktion> auctionlist = (from a in dc.Auktion

我这里有两张桌子: 拍卖和物品

一次拍卖有一件物品。 1.Aricle有x次拍卖

现在,我加载拍卖列表:

            using (APlattformDatabaseDataContext dc = new APlattformDatabaseDataContext())
        {
            List<Auktion> auctionlist = (from a in dc.Auktion
                                         where a.KäuferID == null
                                         select a).ToList();
            return auctionlist;
        }
使用(APlattformDatabaseDataContext dc=new APlattformDatabaseDataContext())
{
List auctionlist=(来自dc.Auktion中的a)
其中a.KäuferID==null
选择一个.ToList();
返回拍卖清单;
}
但是当我想要“txtBla.Text=auction[0].Article.Text”时,它不会被加载。
问题不在于为什么(从逻辑上讲,它没有完全准备好,并且由于DC已关闭而无法加载),而是我如何在不让DC打开的情况下解决这个问题?

您可以执行以下操作:

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

如果您希望像这样加载关联,您应该像这样使用属性

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;
}
使用(APlattformDatabaseDataContext dc=new APlattformDatabaseDataContext())
{
var dlo=新数据加载选项();
options.LoadWith(o=>o.Article);
dc.LoadOptions=dlo;
List auctionlist=(来自dc.Auktion中的a)
其中a.KäuferID==null
选择一个.ToList();
返回拍卖清单;
}

这样,当从数据库中检索到您的
Auktion
对象时,您的文章将被加载。

数据库中的表是否相关?非常简单,谢谢。我必须在每个新打开的dc中执行此操作吗?因为您正以这种方式实例化上下文,是的,您必须在每次实例化上下文时设置DataLoadOptions。您可能希望将实例化抽象为包含DLO的助手函数,以便代码仅存在于1个位置。