Entity Framework 6 führt die Interception http://entityframework.codeplex.com/wikipage?title=Interception ein, mit der die SQL angepasst werden kann, um die untergeordneten Elemente zu filtern.
Vor der Ausführung Ihrer Anfrage ein Abfangjäger hinzuzufügen und zu entfernen, wenn es nicht relevant ist:
var interceptor = new ActiveTagsInterceptor();
DbInterception.Add(interceptor);
documents = context.Documents
.AsQueryable()
.Include(d => d.Tags)
DbInterception.Remove(interceptor);
Probe Interceptor, die "[Aktiv] = 1 Und" wenn loading Tags ergänzt:
public class ActiveTagsInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
// [Tag] AS [Extent6] ON => [Tag] AS [Extent6] ON [Extent6].[Active] = 1 And
const string pattern = "\\[Tag\\]\\sAS\\s\\[([\\w]+)\\]\\sON";
const string replacement = "$& [$1].[Active] = 1 And ";
command.CommandText = Regex.Replace(command.CommandText, pattern, replacement);
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
}
Ich habe vergessen zu erwähnen, dass ich keinen anonymen Typ zurückgeben möchte ... Ich muss ein Objekt (oder eine Sammlung) vom Typ tblParent zurückgeben. –
Sicher, ich habe ein weiteres Code-Snippet hinzugefügt, das Ihnen ein stark typisiertes Parent-Objekt gibt, das alle untergeordneten Elemente enthält, die den Kriterien entsprechen. Bitte guck dir das an. –
Das macht Sinn ... wir sind jedoch aus dem Bereich des eifrigen Ladens ausgezogen. –