Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据单词的长度对单词进行分组#_C#_Linq_C# 4.0 - Fatal编程技术网

C# 根据单词的长度对单词进行分组#

C# 根据单词的长度对单词进行分组#,c#,linq,c#-4.0,C#,Linq,C# 4.0,是否可以根据字串的长度将其分组到数组中?我正在尝试以下代码,但它不起作用。多谢各位 string [] groups = from word in words.split(' ') orderby word ascending group word by word.Length into lengthGroups orderby lengthGroups.Key descending select new { Length = leng

是否可以根据字串的长度将其分组到数组中?我正在尝试以下代码,但它不起作用。多谢各位

string [] groups =
      from word in words.split(' ')
      orderby word ascending
      group word by word.Length into lengthGroups
      orderby lengthGroups.Key descending
      select new { Length = lengthGroups.Key, Words = lengthGroups };

由于查询返回匿名类型的列表,因此应使用
var
关键字:

var sentence = "Here are a few words in a sentence";
var words = sentence.Split(' ');

var groups =
  from word in words
  orderby word ascending
  group word by word.Length into lengthGroups
  orderby lengthGroups.Key descending
  select new { Length = lengthGroups.Key, Words = lengthGroups };

// Test the results
foreach (var lengthGroup in groups)
{
    Console.WriteLine(lengthGroup.Length);

    foreach(var word in lengthGroup.Words)
    {
        Console.WriteLine(word);
    }
}
您还可以使用动态
IEnumerable

IEnumerable<dynamic> groups =
  from word in words
  orderby word ascending
  group word by word.Length into lengthGroups
  orderby lengthGroups.Key descending
  select new { Length = lengthGroups.Key, Words = lengthGroups };

由于查询返回匿名类型的列表,因此应使用
var
关键字:

var sentence = "Here are a few words in a sentence";
var words = sentence.Split(' ');

var groups =
  from word in words
  orderby word ascending
  group word by word.Length into lengthGroups
  orderby lengthGroups.Key descending
  select new { Length = lengthGroups.Key, Words = lengthGroups };

// Test the results
foreach (var lengthGroup in groups)
{
    Console.WriteLine(lengthGroup.Length);

    foreach(var word in lengthGroup.Words)
    {
        Console.WriteLine(word);
    }
}
您还可以使用动态
IEnumerable

IEnumerable<dynamic> groups =
  from word in words
  orderby word ascending
  group word by word.Length into lengthGroups
  orderby lengthGroups.Key descending
  select new { Length = lengthGroups.Key, Words = lengthGroups };


那么,如何将组写回数组?@Kevin,我想你忘了拆分字符串,我在写?@Qaesar-当你将组全部放在一个数组中时,你打算如何在组之间进行区分?如果你想要的是长度的话,你可以简单地对它们进行排序。上面的代码将返回一个对象列表,每个对象都有一个长度和一个与长度匹配的单词列表。这些对象不是字符串。如果你能给我们举一个你预期产出的例子,也许会有所帮助?:)@凯文,我试过你的代码,但是word.length有问题。那么,如何将组写回数组?@Kevin,我想你忘了拆分字符串,我在写?@Qaesar-当你将组全部放在一个数组中时,你打算如何在组之间进行区分?如果你想要的是长度的话,你可以简单地对它们进行排序。上面的代码将返回一个对象列表,每个对象都有一个长度和一个与长度匹配的单词列表。这些对象不是字符串。如果你能给我们举一个你预期产出的例子,也许会有所帮助?:)@凯文,我试过你的代码,但是word.length有问题。linq group words by length的第一个谷歌结果之一:是的,但它对我不起作用。这就是我问的原因。看起来你已经改变了你的问题,表明你正在使用
var
而不是
string[]
。这种情况使我现在的答案无效。它解决了你的问题吗?如果是这样,我会将问题改回
string[]
,并接受答案,以便此页面对其他查看此页面的人有意义。是的,Kevin,我提到我已更改了问题。是的,我同意你的看法。谢谢。第一个“linq group words by length”的谷歌搜索结果之一:是的,但它对我不起作用。这就是我问的原因。看起来你已经改变了你的问题,表明你正在使用
var
而不是
string[]
。这种情况使我现在的答案无效。它解决了你的问题吗?如果是这样,我会将问题改回
string[]
,并接受答案,以便此页面对其他查看此页面的人有意义。是的,Kevin,我提到我已更改了问题。是的,我同意你的看法。谢谢。@Xander,我使用的是包含字数的单个字符串,而不是字符串数组。我已经更新了我的问题,我使用了words.split。是正确的吗?@Qaesar你为什么更新你的问题?关于发布的答案有什么不明白的地方?因为我不想先创建数组,所以我想直接根据单个字符串的长度将其分成若干组。@Qaesar您在引擎盖下分配了一个
字符串[]
。它必须是一个数组才能分组!你为什么要这么做?这并不会使您的代码更具可读性/可维护性…@Xander,我使用的是包含字数的单个字符串,而不是字符串数组。我已经更新了我的问题,我使用了words.split。是正确的吗?@Qaesar你为什么更新你的问题?关于发布的答案有什么不明白的地方?因为我不想先创建数组,所以我想直接根据单个字符串的长度将其分成若干组。@Qaesar您在引擎盖下分配了一个
字符串[]
。它必须是一个数组才能分组!你为什么要这么做?它不会使您的代码更具可读性/可维护性。。。