Hey Fabio,
thanks for that- it made me go and look on the differences between
IEnumerable<T> and IQueryable<T>, so I had a very important lesson as
well :)
As for my question, here's a piece of code I just wrote to express
better what I have in mind.
I'd be, yet again, more than grateful to get any feedback and ideas-
does this make any sense??
Or would having a method in my DAL per search criteria that returns an
IQueryable<TEntity> would be easier to maintain?
public static class ExpressionCriteriaExtensions
{
public static Expression<Func<T, bool>> GetCriteria<T>(this
Expression<Func<T, bool>> ret, T instance, params Expression<Func<T,
object>>[] criteriaProperties)
{
foreach (var expression in criteriaProperties)
{
Func<T, object> func = expression.Compile();
object propertyValue = func(instance);
ret = ret.GetStringEqCriteria(func, propertyValue);
ret = ret.GetDateEqCriteria(func, propertyValue);
}
return ret;
}
private static Expression<Func<T, bool>> GetStringEqCriteria<T>
(this Expression<Func<T, bool>> ret, Func<T, object> func, object
propertyValue)
{
string stringValue = propertyValue as string;
if (!string.IsNullOrEmpty(stringValue))
{
Func<T, object> tmp = func;
if (tmp != null)
{
if (ret == null) ret = t => tmp(t) == stringValue;
else
{
Func<T, bool> currentCriteria = ret.Compile();
ret = t => currentCriteria(t) && tmp(t) ==
stringValue;
}
}
}
return ret;
}
private static Expression<Func<T, bool>> GetDateEqCriteria<T>
(this Expression<Func<T, bool>> ret, Func<T, object> func, object
propertyValue)
{
if (propertyValue is DateTime)
{
DateTime dateTimeValue = (DateTime)propertyValue;
Func<T, object> tmp = func;
if (tmp != null)
{
if (ret == null) ret = t => (DateTime)tmp(t) ==
dateTimeValue;
else
{
Func<T, bool> currentCriteria = ret.Compile();
ret = t => currentCriteria(t) && (DateTime)tmp
(t) == dateTimeValue;
}
}
}
return ret;
}
}
On the service, this would be called like that:
Expression<Func<User,bool>> criteria = null;
criteria = criteria.GetCriteria(user, u => u.Name, u => u.Birthdate);
List<User> list = myDal.GetBy(criteria);
and in MyDal I'll have this:
public IList<User> GetBy(Expression<Func<User, bool>> criteria)
{
return Session.Linq<User>().Where(criteria).ToList();
}
On Nov 13, 11:59 pm, Fabio Maulo <[email protected]> wrote:
> btw should be
> Expression<Func<TEntity, bool>> predicate
> to be used as
> Session.Linq<User>.Where(predicate)
>
> 2009/11/13 Nieve <[email protected]>
>
>
>
>
>
> > Hello all,
> > I'm working on an ASP.NET MVC app, using linq-to-nh and soa.
> > Thing is, when we get the user entering serach criteria, we have the
> > search criteria values in the service and need to send them to the DAL
> > in order to use them on linq where methods and the likes. Before we
> > also need to verify the values aren't null or empty. Example:
> > we got an instance of User in our service (Name="Jon", Surname="",
> > Birthdate, AddressNumber...)
> > I would like to have a way to send Func<User, bool> to my DAL where
> > I'll do my Session.Linq<User>.Where(func) instead of having in my DAL
> > a method per search criteria (GetByName, GetByBirthdate,
> > GetWhereBornAfter etc').
>
> > Does this make any sense? Or would you let the DAL deal with all this
> > logic?
> > I was thinking of having a class that verifies the specified
> > properties aren"t null/empty and then if they're not sends back the
> > needed Func<T, bool> search criteria (ie user => user.Name == "Jon" &&
> > user.Birthdate == birthdate && user.AddressNumber == number;)
>
> > Any kind of feedback would be highly appreciated!
>
> --
> Fabio Maulo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---