2011-03-17 6 views
12

Tengo problemas con GROUP BY y tengo ganas de cargar. Intento explicar lo que estoy haciendo. estoy consultando una CTX DataContext para eventosLinq/EF, Eager loading y GROUP BY issues

La clase de evento tiene las siguientes propiedades

string Description 
DateTime Date 
bool IsDeleted 
Guid SubjectId 
string Title 
DateTime Created 
Guid ForProjectId 

Person TriggeredBy 
List<Guid> Targets 

Hay eventos muttiple con el mismo SubjectId y me gustaría llegar a tener eventos con SubjectIds únicas y que las son los más nuevos en el grupo. Termino con la siguiente consulta.

var events = from x in 
      (from e in ctx.Events 
       .Include("TriggeredBy") 
       .Include("Targets") 
       group e by e.SubjectId 
       into g 
       select new 
        { 
         GroupId = g.Key, 
         EventsWithSameSubjectId = g, 
        } 
      ) 
       select x.EventsWithSameSubjectId 
       .OrderByDescending(y => y.Created).FirstOrDefault(); 

La consulta compila bien y devuelve el conjunto resultante correcto. Pero las propiedades incluidas son siempre nulas.

Cuando me tira la consulta para ver si eagor carga está funcionando correctamente ....

var events = (from e in ctx.Events.OfType<DataNotificationEvent>() 
       .Include("TriggeredBy") 
       .Include("Targets") 
       select e).ToList(); 

Este devolver los eventos con todas las propiedades incluidas.

Este es un problema/error conocido con Linq/EF o hay alguna forma en que puedo deshacerme de este error.

Saludos

Vicente Ottens

+0

Hay un problema con la carga ansiosa cuando se combina con la agrupación (y subselects y proyecciones). Eche un vistazo a esta pregunta: http://stackoverflow.com/questions/4474951/entity-framework-include-is-not-working/4475504#4475504 Los dos artículos vinculados en la respuesta pueden ayudarlo a encontrar una solución alternativa. – Slauma

Respuesta

9

planea proyectar en un tipo anónimo, por lo Include() no va a trabajar de esa manera. Porque lo que ha hecho con el group y la proyección en el tipo anónimo es cambiar la forma de la consulta. Eso arroja la carga ansiosa. Leer this article podría ayudar.

2

Thnx para la respuesta rápida. Me apuntaste en la dirección correcta. Aquí está la solución que se me ocurrió:

using MyFunc = Func<ExtendedCoreContext, Guid, IQueryable<DataNotificationEvent>>; 

private static readonly MyFunc GetMentionsNewCompiledQuery = 

    CompiledQuery.Compile<ExtendedCoreContext, Guid, IQueryable<DataNotificationEvent>>(

     (ctx, personId) => ((ObjectQuery<DataNotificationEvent>)(
      from x in (
       from e in ctx.Events.OfType<DataNotificationEvent>() 
       group e by e.SubjectId 
       into g 
       select g.OrderByDescending(p => p.Created).FirstOrDefault() 
      ) 
      orderby x.Created descending 
      where x.Targets.Any(t => t.Id == personId) 
      select x 
     )) 
      .Include(EntityProperties.Event.TriggeredBy) 
      .Include(EntityProperties.DataNotificationEvent.Targets) 

    );