简体   繁体   English

如何通过分隔符和多个分组来分割字符串并在Linq中对它们进行计数?

[英]How to split string by delimeter and multiple group by and count them in Linq?

I have a LogData List and which is designed like below. 我有一个LogData列表,其设计如下。

public class LogDataEntity
{
   public string IndexPattern;
   public int Type;
   public LogDataEntity(string pattern , int type)
   {
         IndexPattern = pattern;
         Type = type;
   }
}


List<LogDataEntity> list = new List<LogDataEntity>();
list.add(new LogDataEntity("1,2,9,10", 2));    
list.add(new LogDataEntity("1,10", 1));
list.add(new LogDataEntity("1,2,3", 2));
list.add(new LogDataEntity("3,9,10", 3));
list.add(new LogDataEntity("9,10", 2));
list.add(new LogDataEntity("9,10", 2));

And i want the result like below. 我想要下面的结果。

 [Index]   [Type]    [Count]
   10    :   2          3      
   9     :   2          3     
   1     :   2          2      
   3     :   1          2     
   2     :   2          2 
   3     :   3          1
   9     :   3          1
   10    :   3          1
   1     :   1          1

I want to group by and count not only splited string(indexpattern) but also type too. 我想分组并不仅计算分割的字符串(indexpattern),而且还要键入。 And i want to count and show them by OrderByDescending(Count). 我想通过OrderByDescending(Count)进行计数并显示它们。

I think There is multiple group by. 我认为有多个分组依据。 How should i do this with Linq? 我应该如何用Linq做到这一点?

You can use SelectMany to create list of (Index, Type) pairs, then group by and count to do the rest: 您可以使用SelectMany创建(索引,类型)对的列表,然后进行分组和计数以完成其余工作:

var pairs = data.SelectMany(x => x.IndexPattern
                                  .Split(",")
                                  .Select(y => new {Index = y, Type = x.Type});

var res = from p in pairs
          group p by new { p.Index, p.Type } into grp
          select new {
            Index = grp.Key.Index,
                    grp.Key.Type,
                    grp.Count()
          };

(An order by clause can be added before the final Select as required.) (可以根据需要在最终的Select之前添加order by子句。)

You've, probably, stuck in SelectMany ; 您可能已经陷入SelectMany all the other commands are quite evident: 所有其他命令都很明显:

var result = list
  .SelectMany(record => record 
     .IndexPattern
     .Split(',')
     .Select(item => {
        index = item,
        type = record.Type, 
      }))
   .GroupBy(item => item)
   .OrderByDescending(chunk => chunk.Count())
   .Select(chunk => $"{chunk.index,-10} : {chunk.type,-10} {chunk.Count()}");  

Console.WriteLine(string.Join(Environment.NewLine, result));

This is improve version or previous answers. 这是改进版本或以前的答案。

var pairs = Logs.SelectMany(x => x.IndexPattern.Split(',').Select(y => new { 
Index = y, Type= x.Type }));

var pairs2 = (from p in pairs group p by p into grp select new { Index = 
grp.Key.Index, Reason = grp.Key.Type, Count = grp.Count() }
            ).OrderByDescending(p => p.Count);


foreach (var i  in pairs2) 
{

   //print with i
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM