[英]LINQ Query to inner join DbContext Entities to IQueryable
I have two tables, 'Events' and 'Services', that have a one-to-many relationship (each event has at least one service). 我有两个表,'Events'和'Services',它们具有一对多的关系(每个事件至少有一个服务)。
I'm writing a C# console application that will extract a subset of events and then extract the corresponding services. 我正在编写一个C#控制台应用程序,它将提取事件的子集,然后提取相应的服务。
I have a method that extracts the events into an IQueryable<Event>
object and it's working as expected. 我有一个方法将事件提取到
IQueryable<Event>
对象,它按预期工作。 But when I join that IQueryable<Event>
object to an IQueryable<Service>
(as shown below) my resulting object contains references different contexts and I only want it to contain the IQueryable<Service>
results. 但是当我将
IQueryable<Event>
对象加入IQueryable<Service>
(如下所示)时,我的结果对象包含不同上下文的引用,我只希望它包含IQueryable<Service>
结果。
Is there a better way to do this? 有一个更好的方法吗?
Here's my 'ExtractServices' method: 这是我的'ExtractServices'方法:
public IQueryable<Service> ExtractServices(IQueryable<Event> events)
{
using (var preCertEntities = new PreCertEntities())
{
IQueryable<Service> services = from s in preCertEntities.Services
orderby s.EventId
select s;
services = from s in services
join e in events on s.EventId equals e.EventId
select s;
return services;
}
You shouldn't need to extract child relationships like this - EF does it for you: 你不应该像这样提取子关系--EF会为你做:
public IQueryable<Service> ExtractServices(IQueryable<Event> events)
{
return events.SelectMany(e => e.Services).OrderBy(s => s.EventId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.