LINQ to DataSet 不区分大小写分组依据
我有一个数据表,我想通过数据表的列(例如字符串类型的 Column1
)执行不区分大小写的分组。我观察到,通常 LINQ to DataSet 会执行区分大小写的比较。例如,如果 Column1
有两个字符串值“Test”和“test”,则在应用 group by
后,它会返回两个单独的行,其值为“Test”和“test” ,而不是一个。
查询是:
var countGroupQuery = from table in dataTable.AsEnumerable()
group table by table.Field<string>(Column1) into groupedTable
select new
{
value = groupedTable.Key,
count = groupedTable.Count()
};
是否有任何方法可以执行不区分大小写的group by
,以便在上面的示例中我只得到一行具有一个值(“Test”或“test”)? ToUpper
或 ToLower
实际上会将值更改为大写或小写,而不是使用至少一个输入值,所以我不想使用它:
group table by table.Field<string>(Column1).ToUpper() into groupedTable
I have a data table and I want to perform a case insensitive group by over a column of data table (say Column1
of type string). I observed that normally LINQ to DataSet performs a case sensitive comparison. For example, if Column1
has two string values "Test" and "test", after applying group by
it returns two separate rows with the values "Test" and "test", instead of one.
The query is:
var countGroupQuery = from table in dataTable.AsEnumerable()
group table by table.Field<string>(Column1) into groupedTable
select new
{
value = groupedTable.Key,
count = groupedTable.Count()
};
Is there any method to perform a case-insensitive group by
so that in the above example I get only one row with one value (either "Test" or "test")? ToUpper
or ToLower
would actually change the values to either upper case or lower case instead of using at least one of the input values, so I don't want to use this:
group table by table.Field<string>(Column1).ToUpper() into groupedTable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法通过查询表达式执行此操作,但可以使用点表示法来执行此操作:
您甚至可以使用更复杂的
GroupBy
重载在一次调用中执行此操作:显然,这是使用不变区域性- 您还可以使用当前的文化或顺序规则。
You can't do this from a query expression, but you can do it with dot notation:
You can even use a more complicated overload of
GroupBy
to do it in one call:Obviously that's using the invariant culture - you could also use the current culture or ordinal rules.
这篇 MSDN 文章提供了一些有关数据集和区分大小写的信息。
This MSDN article has some information on datasets and case sensitivity..