动态Linq:如何指定StringComparison类型?
我正在根据从客户端浏览器发送的排序字段集合对数据集进行一些自定义过滤和排序,并使用 Dynamic Linq 来实现(大部分)所需效果。我遇到的问题是当我尝试按字符串类型的列进行排序时,该列既包含传统字符串也包含存储为字符串的数字。我似乎无法传入 StringComparison 枚举值,或为 Dynamic Linq orderby 函数指定 IComparer 参数。
我的排序代码看起来像:
myList.AsQueryable().OrderBy("StringColWithNums ASC")
我最终得到:
1
10
100
11
12
2
20
而不是:
1
2
10
11
12
20
100
有人有做类似事情的经验吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这些是对象,则需要进行一些调整,只需使用 int.Parse(r.StringColWithNums) 或任何字段。
哎呀,抱歉,没有阅读所有 OP 来查看它也有字母,并且您想要动态 linq,编辑
编辑
我不知道您是否能够使用动态 linq 和传递 IComparer。您也许可以在获得结果后执行此操作(即,正如我最初编写的排序,经过修改)。如果您想追寻这条路线,请发表评论。
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.
这是尝试在字符串比较中执行数字比较的一个基本问题。我可以通过以下几种方法来做到这一点:
加载列表时,在数字前加上一定数量的零,以伴随最大字符串大小,即
String.Format("000000", number)
。仅当您主要关心排序而不关心结果的外观时,这才有效 - 即使这样,您也可以将“000010”转换回数字并调用 ToString() 方法以再次显示数字,而无需前导零。< /p>编写您自己的 OrderBy 实现(扩展方法),其中您传递一个函数(或匿名函数)作为参数,以调用传入的方法对结果重新排序。
This is a fundamental problem with attempting to perform numeric comparisons within a string comparison. A couple of ways I would do this:
When loading the list, prefix numbers with an amount of zeroes that will accompany the max string size, i.e.
String.Format("000000", number)
. This will only work if you care mostly about sorting and less about the appearance of the results - even then, you could convert "000010" back to a numeric and call the ToString() method to display the number again without the leading zeroes.Write your own implementation (extension method) of OrderBy wherein you pass a function (or anonymous function) as a parameter to re-sort the results calling the method passed in.
您可以通过编写一个新的字符串比较器来解决这个问题
然后您可以在 OrderBy 和 Sort 等方法中使用比较器
这将为您提供所需的结果。如果没有,那么只需调整比较器即可。
You can solve this by writing a new string comparer
Then you can use the comparer in methods like OrderBy and Sort
This will give you the desired result. If not then just tweak the comparer.
看来这不是可以用 Dynamic Linq 开箱即用完成的事情,至少在 .NET 2.0/3.5 中不能。我最终修改了 Dynamic Linq 源代码以完成我所需要的。
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.