2

I have a function with two strings. Can't figure out how to incorporate those two strings into a LINQ query.

Here is my function:

private void DataBind_GridView_Search(string OptionalArgsSortExpression = "", string OptionalArgsSortDirection = "")
{
List<mainSearchDataModel> Query = GetData();
if (Query != null)
{


    /* Problem ... */
    Query = from x in Query
            orderby OptionalArgsSortExpression.ToString() OptionalArgsSortDirection.ToString()
            select x;
    /* Problem ... */


    GridView_Search.DataSource = Query;
    GridView_Search.DataBind();
}

Any comments would be highly appreciated.

enter image description here

This is the error I get. Also I am not using DLINQ.

'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

UPDATE:

Is there a way to ORDER a List or IEnumerable either one. It seems everything I try does not work.

There is a way to convert it to DataView but I run into trouble there as well and would rather not go that way.

AK_
  • 7,981
  • 7
  • 46
  • 78
  • Why not pass a `Func` and a `bool` to determine if it should be ascending or descending? – juharr Jan 30 '15 at 20:02
  • @juharr This is the event handler for a gridview sort; the string is passed by the gridview. It can't be made to pass a `Func` instead. – Servy Jan 30 '15 at 20:03
  • 1
    have fun: http://stackoverflow.com/questions/1689199/c-sharp-code-against-a-property-using-the-property-name-as-a-string – AK_ Jan 30 '15 at 20:13

3 Answers3

0

You could try something like this (Provided that you use DLINQ, as Servy pointed out in his comment.):

var query = query.OrderBy(String.Format("{0} {1}",OptionalArgsSortExpression,OptionalArgsSortDirection));
Community
  • 1
  • 1
Christos
  • 53,228
  • 8
  • 76
  • 108
  • 3
    That's assuming he's already using DLINQ, which there's no indication that he is. – Servy Jan 30 '15 at 20:00
  • @Servy I am about to include this into my answer. I haven't any other idea. – Christos Jan 30 '15 at 20:01
  • 1
    That certainly is the difficulty in finding high quality answers. The best answers should take into account the constraints of the problem. – maxwellb Jan 30 '15 at 20:19
  • @maxwellb I don't claim that my answer or any of my answers are the best. Either provide a solution or not. Furthermore, I updated my answer including Servy's comment. – Christos Jan 30 '15 at 21:02
  • @Christos I'm not mentioning that to be personal, but as a statement of challenges facing the format of the site. We strive to work as a community to moderate both high quality questions and high quality answers. In this case, through comments and feedback, the OP is not using DLINQ, so an answer depending on DLINQ is not answering the OP's question. – maxwellb Jan 31 '15 at 05:29
0

You could use my OrderByString extension. It converts strings into calls to the actual OrderBy methods, so it is compatible with all Linq providers.

using OrderByExtensions;

Query = Query.OrderBy(OptionalArgsSortExpression.ToString()
     + " " + OptionalArgsSortDirection.ToString())

https://github.com/Grax32/OrderByString

https://www.nuget.org/packages/OrderByString/

Grax32
  • 3,986
  • 1
  • 17
  • 32
  • I would go that way but due to how my project is we are not allowed to add Packages that are not approved. It is helpful but I can't use it. –  Jan 30 '15 at 22:17
  • You can always grab the source files from github and put them in your project. I released this one as public domain, so there are no encumbrances preventing you from doing that. – Grax32 Jan 30 '15 at 22:46
-1

You could go this route

GridView.DataSource = Query.OrderBy(o => o.Field1).ThenBy(t => t.Field2).ToList()
JimmyV
  • 493
  • 3
  • 12