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

时间:2017-10-22 06:35:30

标签: 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 => (o.Count() % 2 == 1))
                  .FirstOrDefault();
Console.WriteLine(result.ToString());

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

您解决问题的方法是错误的,分组值是var hashtags = [ '#hr', '#acc', '#sales' ]; var message = "hello there #hr #acc"; var filteredMessage = _.filter( message.split( ' ' ), value => value.match( hashtags ) ); // suppose to return [ '#hr', '#acc' ] 的实例,因此您必须通过键选择适当的值。

Grouping

因此,在这个例子中,你应该有两个作为返回值。