asp.net-mvc-3linqnhibernateilist

Using Any to check against a list in NHibernate


I am trying to get all @events where that have a FiscalYear.id inIList<int> years. I am using any() but it is throwing the following stacktrace error:

 Unrecognised method call: 
 System.Linq.Enumerable:Boolean Any[TSource](System.Collections.Generic.IEnumerable`1[TSource], System.Func`2[TSource,System.Boolean])

Any Ideas? Here is the code:

FindAllPaged(int eventTypeId, IList<int> aors, IList<int> years)
{

IList<Domain.Event> results =
    session.QueryOver<Event>()
      .Where(@event => !@event.IsDeleted && 
      @event.EventType.Id == eventTypeId &&
      years.Any(y => y == @event.FiscalYear.Id))
}

Solution

  • Try using the Contains method instead:

    IList<Domain.Event> results = session
        .QueryOver<Event>()
        .Where(e => !@event.IsDeleted && 
            @event.EventType.Id == eventTypeId &&
            years.Contains(@event.FiscalYear.Id))
        .ToList();
    

    Or build your restrictions the long way using IsIn:

    IList<Domain.Event> results = session
        .QueryOver<Event>()
        .Where(e => !@event.IsDeleted && @event.EventType.Id == eventTypeId)
        .And(Restrictions.On<Event>(@event => @event.FiscalYear.Id)
                         .IsIn(years.ToArray()))
        .ToList();