根据需要使用导航属性加载数据。如果导航属性不存在(Threshold.Type 和 Threshold.Owner),请打开您的 dbml 并添加关联。
懒惰地使用导航属性会导致额外的查询。
List<Threshold> result = dc.Thresholds.ToList(); //run first query
foreach(Threshold t in result)
{
Type type = t.Type; //run second query (per row)
Owner owner = t.Owner; //run third query (per row)
}
使用 DataLoadOptions 在查询期间急切地加载相关数据。
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Threshold>(t => t.Type);
options.LoadWith<Threshold>(t => t.Owner);
dc.LoadOptions = options;
List<Threshold> result = dc.Thresholds.ToList(); // run query
foreach(Threshold t in result)
{
Type type = t.Type; //these values are already loaded by this point.
Owner owner = t.Owner;
}
编写查询以加载所需形状的数据
var query =
from t in dc.Thresholds
let type = t.Type
let owner = t.Owner
select new {Threshold = t, Type = type, Owner = owner};
var result = query.ToList(); //run query
foreach(var x in result)
{
Threshold t = x.Threshold;
Type type = x.Type; //might be null.
Owner owner = x.Owner; //might be null.
}