【问题标题】:How to get entities that a collection property has any of the elments of a list?如何获取集合属性具有列表任何元素的实体?
【发布时间】:2014-04-10 09:59:25
【问题描述】:
我有这个实体:
MyEntityA
{
long IDEntityA;
List<EntityB> lstEntityB;
.... (other properties);
}
MyEntityB
{
long IDEntityB;
string Name;
.... (other properties)
}
List<long> lstIDsEntitiesB; //this list has many IDs of entities B.
我想获取所有实体 A,其中属性 lstEntitiesB 具有一个或多个 ID 位于 lstIDsEntitiesB 上的实体。
我不知道我是否必须使用连接或有任何其他方式,可能使用任何或包含。
非常感谢。
【问题讨论】:
标签:
entity-framework
join
iqueryable
related-content
【解决方案1】:
class MyEntityA
{
public long IDEntityA;
public List<MyEntityB> lstEntityB;
}
class MyEntityB
{
public long IDEntityB;
public string Name;
}
public class Test
{
List<long> lstIDsEntitiesB;
public void TestAlvaroProblem()
{
List<MyEntityA> entitiesA = new List<MyEntityA>();
IEnumerable<MyEntityA> filteredOut = entitiesA.Where(a => a.lstEntityB
.Select(b => b.IDEntityB).Intersect(lstIDsEntitiesB).Any());
}
}
您应该选择 lstEntityB 的 Id 与 lstIDsEntitiesB 相交的实体 A