获取子嵌套集合实体的元素联合 [英] Get Element Union of Child Nested Collection Entities
问题描述
大家好!
这是我无法弄清楚(也无法通过Google搜索)的内容
鉴于以下实体:
Good day everyone!
Here''s something I''m not able to figure out (and not able to google out)
Given the following Entities:
class EntityA {
public int Aid {get;set}
public ICollection<entitya> AEntities {get;set;}
public ICollection<entityb> BEntities {get;set;}
}
class EntityB {
public int BId {get;set;}
public int AId {get;set;} //ForeingKey
}</entityb></entitya>
所以,我有一个EntityA,它具有:
-子级" EntityA的列表(通常是多对多关系,其中A可以指B,反之亦然)
-EntityB的列表
有没有办法像这样:
So, I have an EntityA which has:
- a list of "children" EntityA (techincally it''s a many to many relationship, in which A can refer to B and vice versa)
- a list of EntityB
is there a way to have something like:
//get my "main" entity
EntityA myEntityA = db.EntityA.Single(m => m.Id = myId);
//get all EntityB items which are children of all of my "related" myEntityA.AEntities
ICollection<entityb> myEntitiesB = myEntityA.AEntities.All.BEntities;</entityb>
最后,myEntitiesB应该包含与我的主要实体相关的任何EntitiesA的所有EntityB子级,但是上面的代码无法编译.也许这只是命令sintax中的一小部分,但是我无法通过google找到解决方案(我发现很难对我需要的内容进行精确"搜索,并且发现了数十个结果页面与我的问题无关)
我可以使用以下方法:
At the end, myEntitiesB should contais all the EntityB children of any of the EntitiesA related to my main entity, but the code above doesn''t compile. Maybe it''s just a little thing in the command sintax, but I''m not able to find a solution with google (I find difficult to do a "precise" search for what I need, and I found dozens of result pages wich don''t relate to my question)
I could use following approach:
int[] MyChilderEntityAIds = {1;2;....;n}; //get the Ids of my EntityA "children"
var q =
from x in db.EntitiesB
where MyChilderEntitiAIds.Contains(x.AId)
select x;
但是我想知道这是否是唯一"方式(编程很少只有一种方式来完成某件事),或者我是否可以采用类似于第一个示例的方式来做到这一点.
如果问题中有不清楚的地方,请通知我.
预先感谢您,
Alberto
but I would like to know if this is the "only" way (programming rarely has only one way to accomplish something) or if I can do it in a way similar to the first example.
If something in the question is not clear, please let me know.
Thank you in advance,
Alberto
推荐答案
这是我为任何人找到的内容:
Here what I found for anyone:
IEnumerable<EntityB> allEntitiesB = myEntityA.AEntities.SelectMany(m => m.BEntitites);
这篇关于获取子嵌套集合实体的元素联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!