C# linq查询的输出-->;System.Linq.Lookup`2+;分组[System.Int32,System.Int32]
我想要奇数出现的数组的数目 这是我的密码。我认为它正在工作,但我无法将预期的数字2作为字符串输出。我得到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 =
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)的谓词,可以做得更好