我有一个自定义方法获取与jqGrid一起使用的用于搜索的选择列表的字符串。jqGrid选择列表的自定义Linq扩展方法
所需的字符串格式为":All;value1:text1;value2:text2"
我目前有以下方法可重复使用的方式来实现这一目标:
public static string jqGridFilterSelectList<TSource>(this IQueryable<TSource> source,Expression<Func<TSource, string>> selector)
{
return string.Join(";", source.Select(selector).Distinct().ToArray());
}
该工程确定,但它需要调用是这样的:
string filterSelectList = service.GetAllUsers().jqGridFilterSelectList(x=>x.User + ":" + x.User);
我不喜欢使用像x=>x.User + ":" + x.User
这样的选择器。我想这个转换成2个重载像这(伪代码!)
// 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());
}
我想我大概可以做到这一点使用动态LINQ,但我想知道,如果这是可能的。
这不起作用,当尝试'选择器(x)'给出错误“选择器是一个变量,但像方法一样使用”。我试过'x => Expression.Invoke(选择器)+“:”+ Expression.Invoke(选择器)'但它抛出'InvalidOperationException:为lambda调用提供的参数数量不正确 – 2011-03-28 15:59:21
@Paul Creasey,这是一个' Expression'。所以你需要在调用之前编译它:'var myFunc = selector.Compile(); myFunc(x)'或内联:'selector.Compile()(x)'。答复已更改。 – NOtherDev 2011-03-28 16:13:20
无法转换表达式'Table(Finance_RTC_MI_Data).Select(x =>((Invoke(Invoke(value(System.Func'1 [System.Func'2 [com.blah.lvis.Data.DataAccess.Finance_RTC_MI_Data,System。 String)])),x)+“:”)+ Invoke(Invoke(value(System.Func'1 [System.Func'2 [com.blah.lvis.Data.DataAccess.Finance_RTC_MI_Data,System.String]]) ),x)))'到SQL中,并不能把它当作本地表达式。 – 2011-03-28 16:39:01