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# linq查询的输出-->;System.Linq.Lookup`2+;分组[System.Int32,System.Int32]_C#_Linq_Format_Output - Fatal编程技术网

C# linq查询的输出-->;System.Linq.Lookup`2+;分组[System.Int32,System.Int32]

C# linq查询的输出-->;System.Linq.Lookup`2+;分组[System.Int32,System.Int32],c#,linq,format,output,C#,Linq,Format,Output,我想要奇数出现的数组的数目 这是我的密码。我认为它正在工作,但我无法将预期的数字2作为字符串输出。我得到 System.Linq.Lookup`2+Grouping[System.Int32,System.Int32] 相反 int[] array = { 0, 0, 1, 1, 2 }; var result = array.GroupBy(a => a) .Select(o => o) .Where(o =

我想要奇数出现的数组的数目

这是我的密码。我认为它正在工作,但我无法将预期的数字2作为字符串输出。我得到

System.Linq.Lookup`2+Grouping[System.Int32,System.Int32]
相反

int[] array = { 0, 0, 1, 1, 2 };
var result = array.GroupBy(a => a)
                  .Select(o => o)
                  .Where(o => (o.Count() % 2 == 1))
                  .FirstOrDefault();
Console.WriteLine(result.ToString());

您解决问题的方法是错误的,分组的值是
分组的实例
,因此您必须按键选择适当的值

int[] array = { 0, 0, 1, 1, 2 };
var result = array.GroupBy(a => a)
                  .Where(o => (o.Count() % 2 == 1))
                  .Select(o => o.Key);
string resultString = string.Join(", ", result.ToArray());
Console.WriteLine(resultString);
因此,在本例中,应使用两个作为返回值。

尝试以下操作:

var result = array.GroupBy(a => a)
    .Where(o => o.Count() % 2 == 1)
    .FirstOrDefault().Key;

通过传递
FirstOrDefault
array.GroupBy(a=>a).FirstOrDefault(o=>o.Count()%2==1.Key)的谓词,可以做得更好