如何在linq中选择每个拆分的字符串和组与另一个成员?

sum*_*ion 5 c# linq

如何在linq中选择多个拆分字符串和另一个成员?

class LogData
{
  public string IndexPattern {get; set;}
  public string Version {get;set;}
  public string Type1 {get; set;}
  public string Type2 {get; set;}

  //here has a constructor of this class
}
Run Code Online (Sandbox Code Playgroud)

我有一个日志数据列表,其中包含许多日志数据.索引模式是使用分隔符","的日志索引集.

List<LogData> logList = new List<LogData>();
logList.add(new LogData("1,2,4", "Ver1", "pro" , "etc" ) );
logList.add(new LogData("1", "Ver2", "pro" , "etc" ) );
logList.add(new LogData("2", "Ver1", "pro" , "etc" ) );
logList.add(new LogData("1,2,4", "Ver1", "pro" , "etc" ) );
logList.add(new LogData("1,5", "Ver2", "pro" , "set" ) );
Run Code Online (Sandbox Code Playgroud)

我希望拆分索引模式和所有成员分组这样.

[Index] [Version] [Type1] [Type2] [Count]
  1        Ver1     pro      etc    2
  2        Ver1     pro      etc    2
  4        Ver2     pro      etc    2
  1        Ver2     pro      etc    1
  1        Ver2     pro      set    1
  5        Ver2     pro      set    1
  4        Ver2     pro      set    1
Run Code Online (Sandbox Code Playgroud)

我写这样的linq首先分组..

var LogGroup = HackingLogs.GroupBy(g => new {
              IndexPattern = g.IndexPattern.SelectMany( new { Index = c => c 
               }),  //I must select many to get each splited  string
                g.Version,
                g.Type1,
                g.Type2
 }); //I group by this for each splited string and all member pairs to select 
Run Code Online (Sandbox Code Playgroud)

但它无法分组.所以我不能使用选择.我可以为这个问题找到答案吗?

Rah*_*ngh 3

您可以首先IndexPattern使用SelectMany展平并投影所有其他元素。最后按所有列进行分组并获取计数。这应该会给你预期的输出:-

 var res = logList.SelectMany(x => x.IndexPattern.Split(',')
                                  .Select(z => new 
                                      { 
                                           Index = z, 
                                           Version = x.Version, 
                                           Type1 = x.Type1, 
                                           Type2 = x.Type2 
                                      }))
                   .GroupBy(x => new { x.Index, x.Version, x.Type1, x.Type2 })
                   .Select(x => new
                        {
                            Index = x.Key.Index,
                            Version = x.Key.Version,
                            Type1 = x.Key.Type1,
                            Type2 = x.Key.Type2,
                            Count = x.Count()
                        });
Run Code Online (Sandbox Code Playgroud)

工作小提琴。