New Posts New Posts RSS Feed: complex filters in searchservice
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

complex filters in searchservice

 Post Reply Post Reply
Author
jkattestaart View Drop Down
Newbie
Newbie


Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
Post Options Post Options   Quote jkattestaart Quote  Post ReplyReply Direct Link To This Post Topic: complex filters in searchservice
    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
                    
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
jkattestaart View Drop Down
Newbie
Newbie


Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
Post Options Post Options   Quote jkattestaart Quote  Post ReplyReply Direct Link To This Post Posted: 21-Mar-2013 at 11:09pm
Yes forgot to mention i'm using silverlight. And Sqlfunctions are not available in my DomainServices.SL?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
jkattestaart View Drop Down
Newbie
Newbie


Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
Post Options Post Options   Quote jkattestaart Quote  Post ReplyReply Direct Link To This Post Posted: 30-Mar-2013 at 12:27am
Sorry for the late reply.

Thanks for the info. This will do the job
Back to Top
jkattestaart View Drop Down
Newbie
Newbie


Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
Post Options Post Options   Quote jkattestaart Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
jkattestaart View Drop Down
Newbie
Newbie


Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
Post Options Post Options   Quote jkattestaart Quote  Post ReplyReply Direct Link To This Post Posted: 02-Apr-2013 at 2:09pm
Thx
Back to Top
jkattestaart View Drop Down
Newbie
Newbie


Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
Post Options Post Options   Quote jkattestaart Quote  Post ReplyReply Direct Link To This Post 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);
    }

Edited by jkattestaart - 03-Apr-2013 at 11:55pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down