我有一个表AccountSecurity,它是一个多对多表,涉及到账户实体和证券。当我在下面编写查询时,它返回满足where子句的所有证券。但是,列表中的每个安全实例不再具有对其来自的AccountSecurity的引用。所以当我列出[0].AccountSecurity时,它是空的。有没有包括这些信息?我知道我可以重写查询以返回AccountSecurities,并使用.Include(“Security”)来代替,但我想知道是否可以用另一种方法。
var list = (from acctSec in base.context.AccountSecurities
where acctSec.AccountId == accountId
select acctSec.Security).ToList();
更新
当然,如果我执行两个查询,图形将正确填充,则必须有一种方法一次性完成此操作。
var securities = (from acctSec in base.context.AccountSecurities
where acctSec.AccountId == accountId
select acctSec.Security).ToList();
//this query populates the AccountSecurities references within Security instances returned by query above
var xref = (from acctSec in base.context.AccountSecurities
where acctSec.AccountId == accountId
select acctSec).ToList();