实体框架LINQ获取另一个集合中的所有项目 [英] Entity Framework LINQ Get all items part of another collection
问题描述
从DBContext中获取所有与RelationsCollection中的记录重叠的NWatchRelation记录. 相同的Id,RelatedNodeId和RelationType(枚举:int)应视为匹配项.
Get all the NWatchRelation records from the DBContext that overlap those in the relationsCollection. The same Id, RelatedNodeId, and RelationType (enum: int) should be what's considered a match.
public class NWatchRelation : INWatchRelation
{
public int Id { get; set; }
public int NodeId { get; set; }
public NWatchNode Node { get; set; }
public int RelatedNodeId { get; set; }
public NWatchNode RelatedNode { get; set; }
public NWatch.NWatchRelationType RelationType { get; set; }
}
INWatchRelation[] relationsCollection = GetRelations();
推荐答案
您可以在LINQ to Entities中完全做到这一点的唯一方法是使用Queryable.Concat
像这样:
The only way you can do that fully in LINQ to Entities is to manually compose UNION ALL
query by using Queryable.Concat
like this:
IQueryable<NWatchRelation> query = null;
foreach (var relation in relationsCollection)
{
var m = relation;
var subQuery = db.NWatchRelations
.Where(r => r.Id == m.Id
&& r.RelatedNodeId == m.RelatedNodeId
&& r.RelationType == m.RelationType);
query = query == null ? subQuery : query.Concat(subQuery);
}
但是请注意,这是一种有限的方法,如果relationsCollection
很大,将无法使用.
But please note that it's a limited approach and will not work if the relationsCollection
is big.
这篇关于实体框架LINQ获取另一个集合中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!