0

Cars エンティティ セットがあり、すべての Car には AdvancedCodes のセットがあります。最近、メソッド表記法を使用して、EF ストレージから階層を選択しました。次のように見えました。

repository.Cars.Where(a => a.AgentId == userAgent.AgentId)
      .Include(a => a.AdvancedCodes)
      .Where(a => a.CarId != null).ToList()

すべての車内に AdvancedCodes のコレクションを含む車のコレクションを受け取りました。よかった。ここで、複数の LEFT 結合とより複雑な SELECT を実行するために、LINQ-to-Entities 表記に切り替える必要があります。

from cr in context.Cars
from ac in context.AdvancedCodes.Where(a => a.CarId == cr.CarId).DefaultIfEmpty()
from c in context.Contacts.Where(a => a.AdvancedCodeId == ac.AdvancedCodeId).DefaultIfEmpty()
from ag in context.Agents.Where(a => a.AgentId == ac.AdvancedCodeId).DefaultIfEmpty()
where ac.AgentId == agentId && ac.CarId != null && cr.IsApproved == true
select new CarCodeModel
{
     CarBrand = cr.CarBrand.BrandName,
     RegNumber = cr.RegNumber, 
     ContactDate = c.DateCreated,
     ContactCode = ac.Code,
     CodeCreationDate = ac.DateCreated,
     IsUsed = c.CoId != null,
     RecepientName = ag.AgentName
};

それは問題ありませんが、今回はフラットな 2D データ セットを取得し、これを何らかの方法で反復して、2 つの関連するコレクションに分割する必要があります。.Include メソッドの後のように、同じ 2 レベルのコレクションのセットを取得するために、より洗練されたクエリを作成する方法はありますか?

4

0 に答える 0