I have a custom method for getting a string which is used with jqGrid for select lists for searching.
The required string format is ":All;value1:text1;value2:text2"
I currently have the following method to achieve this in a reusable way:
public static string jqGridFilterSelectList<TSource>(this IQueryable<TSource> source,Expression<Func<TSource, string>> selector)
{
return string.Join(";", source.Select(selector).Distinct().ToArray());
}
This works OK, but it requires the call to be like this:
string filterSelectList = service.GetAllUsers().jqGridFilterSelectList(x=>x.User + ":" + x.User);
I'm not very happy with having to use a selector like x=>x.User + ":" + x.User
. I'd like to convert this into 2 overloads like this (pseudo code!!)
// case where we want the value and text of the select list to be the same
public static string jqGridFilterSelectList<TSource>(this IQueryable<TSource> source,Expression<Func<TSource, string>> selector)
{
return string.Join(";", source.Select(selector + ":" + selector).Distinct().ToArray());
//obviously this won't work, but is it possible in some other way while executing it on the database still? Also is it possible to insist the selector only contains 1 column.
}
//case where we want different text and value
public static string jqGridFilterSelectList<TSource>(this IQueryable<TSource> source,Expression<Func<TSource, string>> textSelector,Expression<Func<TSource, string>> valueSelector)
{
return string.Join(";", source.Select(valueSelector + ":" + textSelector).Distinct().ToArray());
}
I suppose i could probably achieve this using dynamic linq, but i'm interested to know if this is possible.