5

I want to return a list of tickets which are currently in one of the given statuses. There's an array of the enum TicketState (with values Open, InProgress and Finished).

public IEnumerable<Ticket> ReadTickets(TicketState[] states)
{
    return ctx.Tickets.Where(t => states.Contains(t.State)).AsEnumerable();   
}

The following exception appears when I test the method:

Cannot compare elements of type 'Project.BL.Domain.Ticketing.TicketState[]'. Only primitive types, enumeration types and entity types are supported.

I've tried to make a list from the array and to use an array of bytes instead, but I keep getting exceptions.

Does anyone know how I can fix this?

Jens
  • 129
  • 1
  • 7
  • 1
    That code wouldn't even compile - `states.Contains(t.State)` isn't a delegate or an expression tree type... Now we could *guess* that you've just missed out `state =>` but who knows what other changes there are... – Jon Skeet Jul 02 '15 at 08:45
  • http://stackoverflow.com/questions/16615803/only-primitive-types-or-enumeration-types-are-supported-in-this-context, http://stackoverflow.com/questions/15211362/only-primitive-types-or-enumeration-types-are-supported-in-this-context, http://stackoverflow.com/questions/15592042/linq-to-entities-only-primitive-types-or-enumeration-types-are-supported-error, http://stackoverflow.com/questions/7379394/entity-framework-unable-to-create-a-constant-value-of-type-exception, try searching. EF can't translate that LINQ to an SQL query. Which EF version is this? How is `Ticket.State` declared? – CodeCaster Jul 02 '15 at 08:46
  • @JonSkeet, `Contains` can definitely receive a single `T` and check for its existence. No need for delegate or expression. – haim770 Jul 02 '15 at 08:47
  • Thanks, I've edited it. – Jens Jul 02 '15 at 08:48
  • @haim770 there's shorthand for `(t => Foo(t))` as `(Foo)`, but not like OP had. – CodeCaster Jul 02 '15 at 08:49
  • @haim770: Look at version 1 of the question... – Jon Skeet Jul 02 '15 at 08:49

1 Answers1

5

Are you looking for Enumerable.Any?

return ctx.Tickets.Where(t => states.Any(s => t.State == s)).AsEnumerable();   
Jaanus Varus
  • 3,508
  • 3
  • 31
  • 49