Author |
Share Topic Topic Search Topic Options
|
jkattestaart
Newbie
Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
|
Post Options
Quote Reply
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
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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
|
|
jkattestaart
Newbie
Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
|
Post Options
Quote Reply
Posted: 21-Mar-2013 at 11:09pm |
Yes forgot to mention i'm using silverlight. And Sqlfunctions are not available in my DomainServices.SL?
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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
|
|
jkattestaart
Newbie
Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
|
Post Options
Quote Reply
Posted: 30-Mar-2013 at 12:27am |
Sorry for the late reply.
Thanks for the info. This will do the job
|
|
jkattestaart
Newbie
Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
|
Post Options
Quote Reply
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?
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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.
|
|
jkattestaart
Newbie
Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
|
Post Options
Quote Reply
Posted: 02-Apr-2013 at 2:09pm |
Thx
|
|
jkattestaart
Newbie
Joined: 30-Jul-2010
Location: Netherlands
Posts: 37
|
Post Options
Quote Reply
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
|
|