LINQ查询仅计算某些值

时间:2015-09-05 12:58:57

标签: c# linq

我有一个列表,其中包含已转换为字符串的字符串和整数。我试图编写一个LINQ查询,其中所有不同字符串的计数不是整数,例如"你好","嗨,"问候"等等和所有整数的计数,但不是字符串,例如。

List x = { "1", "6", "3", "Hi", "5", "Hello", "Hi" }

输出为:

integer count = 4
              Hi = 2
              Hello = 1

我目前正在对所有不正确的整数进行查询分组,但每个整数都被明确地列出,例如。

  

嗨计数= 2             你好计数= 1             1计数= 1             6计数= 1             3计数= 1             5计数= 1

到目前为止,这是我的查询: - (

var q = from x in output
                    group x by x into g
                    let count = g.Count()
                    orderby count descending
                    select new { Value = g.Key, Count = count };

我试图让另一个循环计数所有不是Hi Hello等的值。

 var integerCount = q.Select(
                x => x.Value != "Hi" 
                || x.Value != "Hello")
                .Count();

但这个数字似乎不正确。无论如何我只能做一个返回我想要的查询吗?

谢谢。

4 个答案:

答案 0 :(得分:1)

你走了:

var counts = list.Aggregate(new { Integer = 0, Other = 0 }, (c, s) =>
{
    int c1 = c.Integer, c2 = c.Other, n;
    if (int.TryParse(s, out n)) c1++; else c2++;
    return new { Integer = c1, Other = c2 };
});
Debug.Print("Integers:{0} Other:{1}", counts.Integer, counts.Other);

答案 1 :(得分:0)

<强>步骤:

  1. 我们应该创建一个List<string>
  2. 的工作集合
  3. 为简单起见,我们将主列表分为两个列表;整数和字符串;通过使用int.TryParse整数解析方法来确定值是否为整数类型。
  4. 我们将列出一个列表,我们将按具有相同元素的元素创建组。 elem => elem
  5. 我们现在将为每个组创建一个Key: ElementValue, and Value: Count字典,并在该组中创建.Count
  6. 代码v1:

    var list = new List<string> { "1", "6", "3", "Hi", "5", "Hello", "Hi" };
    
    int num;
    var integers = list.Where(elem => int.TryParse(elem, out num));
    var strings = list.Where(elem => !int.TryParse(elem, out num));
    
    var dictIntegers = integers.GroupBy(elem => elem).ToDictionary(elem => elem.Key, elem => elem.Count());
    var dictStrings = strings.GroupBy(elem => elem).ToDictionary(elem => elem.Key, elem => elem.Count());
    

    代码v2:

    var list = new List<string> { "1", "6", "3", "Hi", "5", "Hello", "Hi" };
    
    int num;
    var listGroupedByElement = list.GroupBy(elem => elem).ToDictionary(elem => elem.Key, elem => elem.Count());
    var dictIntegers = listGroupedByElement.Where(elem => int.TryParse(elem.Key, out num));
    var dictStrings = listGroupedByElement.Where(elem => !int.TryParse(elem.Key, out num));
    

答案 2 :(得分:0)

List<string> items = { "1", "6", "3", "Hi", "5", "Hello", "Hi" };
var result = items.Select(x => new {
    IsInt = Int32.TryParse(x),
    TextValue = x
});
var integerCount = result.Where(x => x.IsInt).Count();
var countPerText = result.Where(x => !x.IsInt)
    .GroupeBy(x => x.TextValue)
    .Select(group => new {
        Text = group.Key,
        Count = group.Count()
    });

答案 3 :(得分:0)

我设法优化了上面的一些代码来获得结果。但是,谢谢大家的帮助。

var count = list.Aggregate(new { Integer = 0, Hi = 0, Hello = 0, (c, s) =>
                {
                    int c1 = c.Integer, 
                        c2 = c.Hi, 
                        c3 = c.Hello,  
                        n;
                    if (int.TryParse(s, out n)) 
                        c1++;
                    else if (s == "Hi") 
                        c2++;
                    else if (s == "Hello") 
                        c3++;
                    return new { 
                        Integer = c1, 
                        Hi = c2, 
                        Hello = c3, 
                                    };
                });