I have a Kendo UI Grid that I am currently allowing filtering on multiple columns. I am wondering if there is a an alternative approach removing the outer switch statement?
Basically I want to able to create an extension method so I can filter on a IQueryable<T>
and I want to drop the outer case statement so I don't have to switch column names.
private static IQueryable<Contact> FilterContactList(FilterDescriptor filter, IQueryable<Contact> contactList)
{
switch (filter.Member)
{
case "Name":
switch (filter.Operator)
{
case FilterOperator.StartsWith:
contactList = contactList.Where(w => w.Firstname.StartsWith(filter.Value.ToString()) || w.Lastname.StartsWith(filter.Value.ToString()) || (w.Firstname + " " + w.Lastname).StartsWith(filter.Value.ToString()));
break;
case FilterOperator.Contains:
contactList = contactList.Where(w => w.Firstname.Contains(filter.Value.ToString()) || w.Lastname.Contains(filter.Value.ToString()) || (w.Firstname + " " + w.Lastname).Contains( filter.Value.ToString()));
break;
case FilterOperator.IsEqualTo:
contactList = contactList.Where(w => w.Firstname == filter.Value.ToString() || w.Lastname == filter.Value.ToString() || (w.Firstname + " " + w.Lastname) == filter.Value.ToString());
break;
}
break;
case "Company":
switch (filter.Operator)
{
case FilterOperator.StartsWith:
contactList = contactList.Where(w => w.Company.StartsWith(filter.Value.ToString()));
break;
case FilterOperator.Contains:
contactList = contactList.Where(w => w.Company.Contains(filter.Value.ToString()));
break;
case FilterOperator.IsEqualTo:
contactList = contactList.Where(w => w.Company == filter.Value.ToString());
break;
}
break;
}
return contactList;
}
Some additional information, I am using NHibernate Linq. Also another problem is that the "Name" column on my grid is actually "Firstname" + " " + "LastName" on my contact entity. We can also assume that all filterable columns will be strings.
EDIT Remember this needs to work with NHibernate Linq and AST.
-
2Have you seen Predicate Builder?Robert Harvey– Robert Harvey2012年11月27日 00:44:50 +00:00Commented Nov 27, 2012 at 0:44
-
@RobertHarvey - yes but I got hung up trying to resolve the multiple column names.Rippo– Rippo2012年11月27日 12:29:25 +00:00Commented Nov 27, 2012 at 12:29
2 Answers 2
Answering your specific question,
private static IQueryable<Contact> FilterContactList(
FilterDescriptor filter,
IQueryable<Contact> contactList,
Func<Contact, IEnumerable<string>> selector,
Predicate<string> predicate)
{
return from contact in contactList
where selector(contract).Any(predicate)
select contact;
}
In the case of "Name", you call it as;
FilterContactList(
filter,
contactList,
(contact) => new []
{
contact.FirstName,
contact.LastName,
contact.FirstName + " " + contact.LastName
},
string.StartWith);
You should add an overload as,
private static IQueryable<Contact> FilterContactList(
FilterDescriptor filter,
IQueryable<Contact> contactList,
Func<Contact, string> selector,
Predicate<string> predicate)
{
return from contact in contactList
where predicate(selector(contract))
select contact;
}
So you can call it like this for "Company" field.
FilterContactList(
filter,
contactList,
(contact) => contact.Company,
string.StartWith);
This prevents the overhead of forcing the caller to create an array when they only intend to seleect one Field/Property.
What you're probably after is something as follows
To remove that logic completely around defining the selector
and predicate
need more info on how filter is constructed. If possible filter should have the selector
and predicate
as properties for FilterContactList to use that get automatically constructed.
Expanding on that a little,
public class FilterDescriptor
{
public FilterDescriptor(
string columnName,
FilterOperator filterOperator,
string value)
{
switch (columnName)
{
case "Name":
Selector = contact => new []
{
contact.FirstName,
contact.LastName,
contact.FirstName + " " + contact.LastName
};
break;
default :
// some code that uses reflection, avoids having
// a case for every column name
// Retrieve the public instance property of a matching name
// (case sensetive) and its type is string.
var property = typeof(Contact)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(prop =>
string.Equals(prop.Name, columnName) &&
prop.PropertyType == typeof(string));
if (property == null)
{
throw new InvalidOperationException(
"Column name does not exist");
}
Selector = contact => new[]
{
(string)property.GetValue(contact, null)
};
break;
}
switch (filterOperator)
{
case FilterOperator.StartsWith:
Predicate = s => s.StartsWith(filter.Value);
break;
case FilterOperator.Contains:
Predicate = s => s.Contains(filter.Value);
break;
case FilterOperator.IsEqualTo:
Predicate = s => s.Equals(filter.Value);
break;
}
}
public Func<Contact, IEnumerable<string>> Selector { get; private set; }
public Func<string, bool> Predicate { get; private set; }
}
Your FilterContactList
would then become
private static IQueryable<Contact> FilterContactList(
FilterDescriptor filter,
IQueryable<Contact> contactList)
{
return from contact in contactList
where filter.Selector(contract).Any(filter.Predicate)
select contact;
}
-
@Rippo updated code, you obviously need the value we're looking for!M Afifi– M Afifi2012年11月28日 13:51:51 +00:00Commented Nov 28, 2012 at 13:51
-
Interesting, looks like its not playing ball... Could not parse expression 'Invoke(value(System.Func
2[Domain.Model.Entities.Contact,System.Collections.Generic.IEnumerable
1[System.String]]), contact).Any(value(System.Func`2[System.String,System.Boolean]))': Object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'. If you tried to pass a delegate instead of a LambdaExpression, this is not supported because delegates are not parsable expressions.Rippo– Rippo2012年11月28日 15:22:32 +00:00Commented Nov 28, 2012 at 15:22 -
@Rippo can you include the code behind FilterDescriptor please and the stack trace?M Afifi– M Afifi2012年12月01日 00:58:31 +00:00Commented Dec 1, 2012 at 0:58
-
Filter descriptor is from Kendo docs.kendoui.com/api/wrappers/aspnet-mvc/Kendo.Mvc/…Rippo– Rippo2012年12月01日 10:33:38 +00:00Commented Dec 1, 2012 at 10:33
-
Full stack and calling code: gist.github.com/4181453Rippo– Rippo2012年12月01日 10:42:37 +00:00Commented Dec 1, 2012 at 10:42
I think a simple way to do this would be to create a map of property names to Func's:
e.g.
private static Dictionary<string, Func<Contact, IEnumerable<string>>> propertyLookup = new Dictionary<string, Func<Contact, IEnumerable<string>>>();
static ClassName()
{
propertyLookup["Name"] = c => new [] { c.FirstName, c.LastName, c.FirstName + " " c.LastName };
propertyLookup["Company"] = c => new [] { c.Company };
}
And change your code to:
var propertyFunc = propertyLookup(filter.Member);
case FilterOperator.StartsWith:
contactList = contactList.Where(c => propertyFunc(c).Any(s => s.StartsWith(filter.Value));
You could also eliminate the switch altogether by creating a lookup for the matching function as well:
matchFuncLookup[FilterOperator.StartsWith] = (c, f) => c.StartsWith(f);
matchFuncLookup[FilterOperator.Contains] = (c, f) => c.Contains(f);
var matchFunc = matchFuncLookup[filter.Operator];
contactList = contactList.Where(c => propertyFunc(c).Any(s => matchFunc(s, filter.Value));
So, to put it all together:
public class ClassName
{
private static readonly Dictionary<string, Func<Contact, IEnumerable<string>>> PropertyLookup
= new Dictionary<string, Func<Contact, IEnumerable<string>>>();
private static readonly Dictionary<FilterOperator, Func<string, string, bool>> MatchFuncLookup
= new Dictionary<FilterOperator, Func<string, string, bool>>();
static ClassName()
{
PropertyLookup["Name"] = c => new[] { c.FirstName, c.LastName, c.FirstName + " " + c.LastName };
PropertyLookup["Company"] = c => new[] { c.Company };
MatchFuncLookup[FilterOperator.StartsWith] = (c, f) => c.StartsWith(f);
MatchFuncLookup[FilterOperator.Contains] = (c, f) => c.Contains(f);
MatchFuncLookup[FilterOperator.IsEqualTo] = (c, f) => c == f;
}
private static IQueryable<Contact> FilterContactList(FilterDescriptor filter, IQueryable<Contact> contactList)
{
var propertyLookup = PropertyLookup[filter.Member];
var matchFunc = MatchFuncLookup[filter.Operator];
return contactList.Where(c => propertyLookup(c).Any(v => matchFunc(v, filter.Value)));
}
}
NB - Is it not redundant to check c.FirstName if you are also checking (c.FirstName + " " c.LastName) ?
-
Re-reading the @MAfifi's answer, the method is similar - just implemented using lambda's with lookups instead of classes and switch statements. The key advantage of the lookup approach over switch is that adding new functions, or columns requires an easier code change - and its also more extensible (it doesnt all have to be defined in the one class).Brian Flynn– Brian Flynn2012年11月28日 09:22:30 +00:00Commented Nov 28, 2012 at 9:22
-
Thanks for this, I have tried this BUT have run into the following error:
System.InvalidCastException Unable to cast object of type 'NHibernate.Hql.Ast.HqlParameter' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'.
Rippo– Rippo2012年11月28日 13:21:46 +00:00Commented Nov 28, 2012 at 13:21 -
Im not very familiar with NHibernate, but It looks like its having difficulty dealing with the more complex where clause. You might try to modify the query to: contactList.Select(c => new { Contact = c, Values = propertyLookup(c) }) .Where(cv => cv.Values.Any(v => matchFunc(v, filter.Value) .Select(cv => cv.Contact);Brian Flynn– Brian Flynn2012年11月30日 04:17:34 +00:00Commented Nov 30, 2012 at 4:17
-
sorry typo in that query: contactList.Select(c => new { Contact = c, Values = propertyLookup(c) }) .Where(cv => cv.Values.Any(v => matchFunc(v, filter.Value))).Select(cv => cv.Contact);Brian Flynn– Brian Flynn2012年11月30日 04:25:43 +00:00Commented Nov 30, 2012 at 4:25