2

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
}
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
  • When you say `Foo.OrderBy(f => f.Bars .Where(b => b.Optional == null) .Max(b => b.Date));` what is `Foo` ? Is it a local variable of type `Foo` ? Something like `Foo Foo = new Foo();` ? – Eduard Dumitru Sep 10 '13 at 14:44
  • Foo is a DbSet, so actually it should be called Foos. Perhaps I should clearly point that out. I thought it would be obvious from the tags. – Snæbjørn Sep 10 '13 at 14:50

3 Answers3

1

Bar.Optional is a collection, not a single reference. You cannot compare collections with null with LINQ-to-Entities. Instead you must filter by the Bars where the Optional collection does not (!) have Any element:

Foos.OrderBy(f => f.Bars
                   .Where(b => !b.Optional.Any())
                   .Max(b => b.Date));

It might be necessary that you must use Max(b => (DateTime?)b.Date) instead of just Max(b => b.Date) to account for the possible case that the Bars collection of a Foo might be empty and therefore doesn't have a maximum Date. I'm not 100% sure about that. You should test the case of an empty Bars collection explicitly.

Slauma
  • 175,098
  • 59
  • 401
  • 420
0
  Foo.OrderBy(f => f.Bars
 .Where(b => b.Optional == null).AsEnumerable().
 .Max(b => b.Date));
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
0

Given your Foo is a collection of Foos, Try this

Foo.OrderBy(f =>f.Bars.Where(x => x.Optional == null)
                                        .Max(x => x.Date)));
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72