I'm attempting to order Foo
by Bars.Max(Date)
on a subset of Bars
that doesn't have an Optional
.
It's hard to explain in text so here's the query I got so far.
// Foos is of type DbSet<Foo> a "code first" EF entity
Foos.OrderBy(f => f.Bars
.Where(b => b.Optional == null)
.Max(b => b.Date));
That query fails with a NotSupportedException
Cannot compare elements of type 'System.Collections.Generic.ICollection`1'. Only primitive types, enumeration types and entity types are supported.
Model
public class Foo
{
public int Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; } // one to many
}
public class Bar
{
public int Id { get; set; }
public DateTime Date { get; set; }
public virtual Foo Foo { get; set; }
public virtual ICollection<Optional> Optional { get; set; } // zero to many
}
public class Optional
{
// omitted for brevity
}