实体框架LINQ Include - 子实体

时间:2014-06-21 13:43:57

标签: c# linq entity-framework

我从 TransactionDetails

加载子实体UserStarted
var result = (from A in context.Transactions.Include(_ => _.TransactionDetails.Select(us => us.UserStarted))
              select A).SingleOrDefault();

现在我想加载另一个实体,但我不知道如何。该实体类似于UserStarted,它也是来自 TransactionDetails

UserEnded

非常感谢

2 个答案:

答案 0 :(得分:1)

您可以将多个包含谓词链接在一起:

var result = context.Transactions
  .Include(transaction => transaction.TransactionDetails.Select(us => us.UserStarted))
  .Include(transaction => transaction.TransactionDetails.Select(us => us.UserEnded))
  .SingleOrDefault();

答案 1 :(得分:1)

您也可以尝试

 var result = (from A in context.Transactions.Include(_ => _.TransactionDetails.Select(us => new { UserStarted  = us.UserStarted, UserEnded = us.UserEnded }))
select A).SingleOrDefault();