Print Page | Close Window

complex filters in searchservice

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4066
Printed Date: 05-Jun-2024 at 5:38am


Topic: complex filters in searchservice
Posted By: jkattestaart
Subject: complex filters in searchservice
Date Posted: 21-Mar-2013 at 2:00pm
In a search service i want an expression like this, but it throws a LINQ error. de parameter for adddays cannot be a reference to a field.

filter = x => x.Acco.AccoOwnerId == accoOwnerId &&
x.Status == "Reserved" &&
x.Created.Value.AddDays(x.Acco.DaysToExpire.Value).CompareTo(DateTime.Now) < 0;

How do you solve this kind of filters in a search service.
I'm using cocktail with devforce 2010

Thanks in advance
                    



Replies:
Posted By: mgood
Date Posted: 21-Mar-2013 at 2:16pm
Because your LINQ query must ultimately be translated to SQL by EntityFramework, you can't use .NET data functions. Take a look at the SqlFunctions class for this purpose.

http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.aspx


Posted By: jkattestaart
Date Posted: 21-Mar-2013 at 11:09pm
Yes forgot to mention i'm using silverlight. And Sqlfunctions are not available in my DomainServices.SL?


Posted By: mgood
Date Posted: 22-Mar-2013 at 9:22am
If you are using Silverlight, then yes, you can't do this on the client and you will have to do it on the server.

One way to achieve that is by using a named query.

http://drc.ideablade.com/devforce-2012/bin/view/Documentation/named-query


Posted By: jkattestaart
Date Posted: 30-Mar-2013 at 12:27am
Sorry for the late reply.

Thanks for the info. This will do the job


Posted By: jkattestaart
Date Posted: 01-Apr-2013 at 11:22pm
One question though.
I now have two repositories:
Bookings (through the default query) and ExpiredBookings through my new named query.
However if try to link this in Cocktail/Devforce 20110 it looks the named query is not reached when i use _repository.FindAsync. I have to specify my query like this to actually call the named query:

var entityManager = ((ExpiredBookingRepository) _repository).EntityManager;
      
      return entityManager.ExpiredBookings.......

this probably has to do something with IRepository<Booking> stuff which does not recognize my named query. Is there a more elegant way to setup my DomainUnitOfWork to use the standard code?


Posted By: mgood
Date Posted: 02-Apr-2013 at 1:54am
Just subclass Repository<T>, override the GetFindQuery and GetKeyQuery methods and compose the queries using your named query as the base query.


Posted By: jkattestaart
Date Posted: 02-Apr-2013 at 2:09pm
Thx


Posted By: jkattestaart
Date Posted: 03-Apr-2013 at 11:16pm
May be the suggestion is usefull to add an a virtual GetQuery in the Cocktail repository.cs and change the GetFindQueries to use this query.
It will certainly enhance the use of namedqueries.

public virtual IEntityQuery<T> GetQuery()
    {
      return EntityManager.GetQuery<T>();
    }

    //Override ivm NamedQuery
    protected override IEntityQuery<T> GetFindQuery(Expression<Func<T, bool>> predicate, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy, string includeProperties)
    {
      IEntityQuery<T> query = GetQuery();            //EntityManager.ExpiredBookings;

      if (predicate != null)
        query = query.Where(predicate);
      if (orderBy != null)
        query = (IEntityQuery<T>)orderBy(query);
      if (!string.IsNullOrWhiteSpace(includeProperties))
        query = ParseIncludeProperties(includeProperties)
            .Aggregate(query, (q, includeProperty) => q.Include(includeProperty));

      return query.With(DefaultQueryStrategy);
    }



Print Page | Close Window