c# - Custom Linq extension method for jqGrid select list -


I have a custom method to get a string that is used with search jqGrid for the selected lists is.

Required string format ": all; value1: text1; value2: text2" I currently have the following method to get it in a reusable manner:

  public static string jqGridFilterSelectList & lt; TSource & gt; (This IQueryable & lt; TSource & gt; source & lt; function & lt; function of the & lt; source; expression; "," {string.Join (return., Source.Select (selector) .Distinct () toArray ()); }   

This works fine, but calls are required for it:

  string filteringcontrol list = service. Gate all user () JqGridFilterSelectList (x => X.User + ":" + x. Warmer);  x = & gt; X.user + ":" + x.user  I am not very happy with using a selector. I would like to convert it to 2 surcharges like this (pseudo code !!)  
  // where we want the value and text of the selection list for the same public stable string, jqGridFilterSelectList & lt; TSource & gt; (The IQueryable & lt; TSource & gt; source & lt; TSource, string & gt; & gt; selector of the function & lt; function) {string.Join return (",", source.Select (selector + " : "+ Selector") .Distinct (.) ToArray ()); // Clearly this will not work, but what is possible with some other ways while executing it on the database? Apart from this, it is important to say that the selector only includes 1 column. } / / Where we want different text and values ​​public static string jqGridFilterSelectList & lt; TSource & gt; (This IQueryable & lt; TSource & gt; Source, Expression & lt; Func & lt; tssros, string & gt; text; selector, expression & lt; Func & lt; TSource, string & gt; valueSelector) {Return string Joint (";", Select Source (Value Selector + ":" + Text Sealer) .Disting (.) Toure ()); }   

I think I can probably get it using dynamic linq, but I am interested in knowing that this is possible.

After

If I understand correctly, you have almost written it:

 < Code> public static string jqGridFilterSelectList & LT; TSource & gt; (The IQueryable & LT; TSource & gt; source, expression & lt; Func & lt; TSource, string & gt; & gt; selector) {var compiled = selector.Compile (); Return string .joined (";", select source (x => compiled (x) + ":" + compiled (x).) Undoubtedly (). ToArray ()); } / / Where we want different text and values ​​public static string jqGridFilterSelectList & lt; TSource & gt; (This IQueryable & lt; TSource & gt; source, expression & lt; Func & lt; tsOSSR, string & gt; text; selector, expression & lt; Func & lt; TSource, string & gt; valueSelector) {Return string. Join (";", source.Select (x => valueSelector.Compile () (x) + ":" + textselector.Compile () (x)) .Distinct (). ToArray ()); }   

To get the values ​​you need just to invoke for your selector tasks, tell me if I think your question is incorrect.

For the second question, is it possible to emphasize the selector that there is only 1 column in it, no simple answer - the selector can be virtually any valid Func < / Code> which takes TSource and returns string and there is no way to limit it (unless you have

< Strong> edit : OK, so is this some kind of SQL LINQ provider, unit framework? So let's try a bit different. This will work, but do not try to get the first data and grade them on objects: public static string jqGridFilterSelectList & lt; TSource & gt; (this IQueryable & LT; TSource & & Gt; Source, for expression & lt; fx & lt; tssoss, string & gt; & gt; selector) {var compiled = selector.Compile (); return string .join (";", source.distink (). Select ASNEMABLE (). Select (x = & gt; compiled (x) + ":" + compiled (x) .oor ());} / / where we want to separate - Different text and value public static string jqGridFilterSelectList & lt; TSource & gt; (This IQueryable & lt; TSource & gt; Source, Expression & lt; Func & lt; tssros, string & gt; text; selector, expression & lt; Func & lt; TSource, string & gt; valueSelector) {Return string. Join (";", source.Distinct (.) Select AsEnumerable (). (X => valueSelector.Compile () (x) + ":" + + text selector .compile () (x)) .oir ()); }

Extra Too large Call switch between a process process while calling, assuming that your selectors are deterministic and your There is no side effect, you can work on the already acquired TSource item.

Comments