c# - 按单词列表在 LINQ 中的第一个字母分组

标签 c# .net linq

我必须按 LINQ 中单词的第一个字母进行分组。由于我是 LINQ 的新手,所以我不知道如何调试它。

//代码

var words4 = testDS.Tables["Words4"].AsEnumerable();

var wordGroups =
    from w in words4
    group w by w.Field<string>("word")[0] into g
    select new {FirstLetter = g.Key, Words = g };

foreach (var g in wordGroups)
{
    Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);  
    Console.WriteLine(g.Field<string>("word"));

}

它在最后一个 Console.WriteLine 中抛出“无效参数”异常。

最佳答案

尝试在内部添加一个循环,这样您可以获得更准确的结果。

替换:

Console.WriteLine(g.Field<string>("word"));

与:

foreach (var w in g.Words)
{
    Console.WriteLine(w.Field<string>("word"));
}

关于c# - 按单词列表在 LINQ 中的第一个字母分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15424985/

相关文章:

ASP.net core 1.0 web.config 被覆盖导致 CGI 异常

.net - 编写更高效的时钟函数

c# - Mkbundle 无法在 OS X 上编译 C# 应用程序

c# - 对按间隔分隔每个分组的日期时间列表进行分组

c# - 是否有数学公式可以确定井字游戏中有多少个对角线序列? C#

c# - 为什么 C# 将整数类型实现为结构而不是原始类型?

c# - PropertyGrid PaintValue 问题 : How to remove (and paint outside) the standard rectangle?

c# - Umbraco - 以编程方式通过 ID 获取节点

c# - 我如何对 List<int> 进行分组并返回 List<int>

LINQ:在运行时构建 where 子句以包含 OR( || )?