How can I define the StringComparison type in Dynamic Linq?

c# dynamic-linq linq

Question

I'm working on doing some custom filtering and sorting of a dataset, based on a collection of sort fields sent from the client browser, and am using Dynamic Linq to achieve (most of) the desired effect. Where I'm running into a problem is when I try to sort by a column of type String, which contains both traditional strings and numbers stored as strings. It doesn't appear that I can pass in a StringComparison enum value, or specify an IComparer parameter for the Dynamic Linq orderby function.

My sorting code looks like:

myList.AsQueryable().OrderBy("StringColWithNums ASC")

I end up with:

1
10
100
11
12
2
20

instead of:

1
2
10
11
12
20
100

Anyone have any experience doing something similar?

1
2
12/6/2010 5:48:04 PM

Accepted Answer

It seems that this is not something that can be accomplished out of the box with Dynamic Linq, at least not in .NET 2.0/3.5. I ended up modifying the Dynamic Linq source code in order to accomplish what I needed.

0
12/8/2010 4:10:40 AM

Popular Answer

myList.AsQueryable().Sort((r, s) => int.Parse(r).CompareTo(int.Parse(s)));

will take some tweaking if those are objects, just use int.Parse(r.StringColWithNums), or whatever the field is.

Oops, sorry, didn't read all the OP to see it has letters too and you want the dynamic linq, editing

EDIT

I don't know that you're going to be able to do that using Dynamic linq and passing IComparer. You may be able to do it after getting the results (i.e. as I was originally writing the sort, with modifications). Comment if you want to pursue that line.



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow