Tuesday, October 25, 2011

LinqSpecs pain!

LinqSpecs is proving tough to use. There is probably a good reason too. Jorge has noticed that it is in alpha and Craig has noticed that it hasn't been updated in over a year. Boo. We struggled to find a way to append a spec to an existing IQueryable and the class below does allow for that, however, when the query concerns get complicated enough NHibernate fails. Perhaps it is crafting HQL it cannot execute???

public class FooLinqSpecHelper : Specification<Foo>

{

   private readonly Foo Foo;

   

   public IQueryable<Foo> ConcatenateQuery(IQueryable<Foo> existingQuery,

            Specification<Foo> specification)

   {

      IQueryable<Foo> concatenatedQuery =

            existingQuery.Where(specification.IsSatisfiedBy());

      return concatenatedQuery;

   }

   

   public IQueryable<Guid> ConcatenateQueryThenReturnOnlyIds(IQueryable<Foo>

            existingQuery, Specification<Foo> specification)

   {

      IQueryable<Guid> concatenatedQuery =

            existingQuery.Where(specification.IsSatisfiedBy()).Select(p => p.Id);

      return concatenatedQuery;

   }

   

   public override Expression<Func<Foo, bool>> IsSatisfiedBy()

   {

      return c => c == Foo;

   }

}

2 comments:

  1. Linqspecs has been superceded by netfx linqspecs. The netfx project has a much nicer implementation.

    ReplyDelete