简体   繁体   English

LINQ to Entities查询相关实体

[英]LINQ to Entities query on related entities

I have 3 EF ObjectSets: Parents, Childrens and Pets, with 1-to-many associations (Parent can have mayn Children and Children can have many Pets). 我有3个EF对象集:父母,儿童和宠物,具有一对多关联(父母可以有mayn儿童,孩子可以有许多宠物)。 For a given Parent, I need to get all the Pets that this Parent's all Children own. 对于给定的父母,我需要获得该父母所有孩子拥有的所有宠物。

I'm concerned if this query is correct or can it be made more efficient: 我担心此查询是否正确或可以提高效率:

Dim query = From par In context.Parents
            From child In par.Childrens
            From pet In child.Pets
            Where par.parent_id = 1
            Select pet

This query is correct although its a cross-join - is simply the Cartesian product of two sets -. 该查询是正确的,尽管它是cross-join -只是两个集合的Cartesian product -。

You can explicitly specify a join based on equality condition. 你可以明确地指定一个join基于equality的条件。

var query = from par  in context.Parents
            join child  in context.Childrens on child.ParentID equals par.ID
            join pet  in context.Products on child.ID equals pet.ChildID
            Where par.parent_id = 1
            select new { par.Name, par.Id };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM