简体   繁体   English

将IQueryable应用于另一个IQueryable / DbContext

[英]Apply IQueryable to another IQueryable / DbContext

I have an IQueryable<Person> which is created based on some input parameters (sort and filter). 我有一个IQueryable<Person> ,它是根据一些输入参数(排序和过滤器)创建的。

Is possible to apply this IQueryable<T> to a DbSet<T> i have in DbContext ? 可以将此IQueryable<T>应用于我在DbContext拥有的DbSet<T>吗?

For example: 例如:

// create the query from some parameters
IQueryable<Person> query = CreateQuery(parameters);

// apply the query somehow to an existing IQueryable / DbSet
AppContext db = new AppContext();
IEnumerable<Person> result = db.People.ApplyQuery(query).ToList();

Actually the context must be the same. 实际上,上下文必须相同。 Even worst, I had a problem moving a ToList statement from inside a linq query to outside linq query, ie 更糟糕的是,我在将ToList语句从linq查询内部移动到外部linq查询时遇到问题,即

// myQuery is an IQueryable from a different context
// This did not work
IEnumerable<Person> result = db.People.Where(p => myQuery.ToList().Contains(p.Id)).ToList();

// This worked
myList = myQuery.ToList();
IEnumerable<Person> result = db.People.Where(p => myList.Contains(p.Id)).ToList(); 

Really strange isn't it? 真的很奇怪不是吗?

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

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