LINQ to DataSet 不区分大小写分组依据

发布于 2024-08-06 06:25:33 字数 865 浏览 2 评论 0原文

我有一个数据表,我想通过数据表的列(例如字符串类型的 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”)? ToUpperToLower 实际上会将值更改为大写或小写,而不是使用至少一个输入值,所以我不想使用它:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半﹌身腐败 2024-08-13 06:25:33

您无法通过查询表达式执行此操作,但可以使用点表示法来执行此操作:

var query = dataTable.AsEnumerable()
                     .GroupBy(x => table.Field<string>(Column1),
                              StringComparer.InvariantCultureIgnoreCase)
                     .Select(groupedTable => new
                             {
                                 value = groupedTable.Key,
                                 count = groupedTable.Count()
                             });

您甚至可以使用更复杂的 GroupBy 重载在一次调用中执行此操作:

var query = dataTable.AsEnumerable()
                     .GroupBy(x => table.Field<string>(Column1),
                              (key, group) => { value = key, 
                                                count = group.Count() },
                              StringComparer.InvariantCultureIgnoreCase));

显然,这是使用不变区域性- 您还可以使用当前的文化或顺序规则。

You can't do this from a query expression, but you can do it with dot notation:

var query = dataTable.AsEnumerable()
                     .GroupBy(x => table.Field<string>(Column1),
                              StringComparer.InvariantCultureIgnoreCase)
                     .Select(groupedTable => new
                             {
                                 value = groupedTable.Key,
                                 count = groupedTable.Count()
                             });

You can even use a more complicated overload of GroupBy to do it in one call:

var query = dataTable.AsEnumerable()
                     .GroupBy(x => table.Field<string>(Column1),
                              (key, group) => { value = key, 
                                                count = group.Count() },
                              StringComparer.InvariantCultureIgnoreCase));

Obviously that's using the invariant culture - you could also use the current culture or ordinal rules.

醉殇 2024-08-13 06:25:33

这篇 MSDN 文章提供了一些有关数据集和区分大小写的信息。

您可以控制大小写敏感度
过滤、搜索和排序
通过设置数据集的 区分大小写
属性。

This MSDN article has some information on datasets and case sensitivity..

You can control the case sensitivity
of filtering, searching, and sorting
by setting the dataset's CaseSensitive
property.

-小熊_ 2024-08-13 06:25:33
var query = dt.AsEnumerable()
              .GroupBy(r => r.Field<string>("Mes"))
              .Select(g => new { Mes = g.Key, 
                                 Tns = g.Sum(s => Convert.ToDecimal(s.Field<string>("Tns"))) })
              .OrderBy(g => g.Mes.First())
              .Where(g => g.Mes == ddlMes.SelectedValue.ToString());
var query = dt.AsEnumerable()
              .GroupBy(r => r.Field<string>("Mes"))
              .Select(g => new { Mes = g.Key, 
                                 Tns = g.Sum(s => Convert.ToDecimal(s.Field<string>("Tns"))) })
              .OrderBy(g => g.Mes.First())
              .Where(g => g.Mes == ddlMes.SelectedValue.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文