如何获取LINQ根据文化定购? [英] How do I get LINQ to order according to culture?
问题描述
说我已经得到了瑞典文字字符串列表:巴南
,苹果
, apelsin
, druva
Say I've got a list of strings with Swedish words: banan
, äpple
, apelsin
, druva
现在我想获得这个名单排序(保持记住,这是真正的查询一个非常简化版本):
Now I want to get this list ordered (keep in mind that this is a very simplified version of the real query):
var result = from f in fruits // The list mentioned above
orderby f
select f
这会给我: apelsin
,苹果
,巴南
, druva
。然而,根据瑞典字母,我应该得到: apelsin
,巴南
, druva
,苹果
This will give me: apelsin
, äpple
, banan
, druva
. However, according to the Swedish alphabet, I should get: apelsin
, banan
, druva
, äpple
我试图改变 System.Threading.Thread .CurrentThread.CurrentCulture
到 SV-SE
,但没有真正似乎影响它。我必须写我自己的lambda函数,并使用 .OrderBy(...)
或者是有什么我可以做,以保持完整的LINQ?
I tried changing System.Threading.Thread.CurrentThread.CurrentCulture
to sv-SE
but that didn't really seem to affect it at all. Do I have to write my own lambda function and use .OrderBy(...)
or is there something else I can do to keep the LINQ intact?
推荐答案
您无法查询表达式做到这一点,但你可以明确的点符号做到这一点:
You can't do this with a query expression, but you can do it with explicit dot notation:
var result = fruits.OrderBy(f => f, StringComparer.CurrentCulture);
这应该这样做,假设线程的当前文化是正确的。或者:
That should do it, assuming the thread's current culture is correct. Alternatively:
CultureInfo culture = new CultureInfo("sv-SE");
var result = fruits.OrderBy(f => f, StringComparer.Create(culture, false));
这篇关于如何获取LINQ根据文化定购?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!