我的代码仅适用于左侧的第一组,而不适用于右侧的另一组,它给我一个错误:“给定的密钥不在字典中”
使用MAJOR_PSIST开始第二组的正确代码是什么?
这是我的代码:
var data = File
.ReadAllLines(textfiletest)
.Select(x => x.Split(':'))
.Where(x => x.Length > 1)
.GroupBy(x => x[0].Trim())
.ToDictionary(x => x.Key, x => x.First()[1]);
这是我的文本文件{textfiletest}
MINOR_PSIST : 2 MAJOR_PSIST : 8
CRITICAL_PSIST : 60 ECSFB_CALL_CONTROL_TYPE : AUTOMATIC
CALL_CONTROL_TYPE : AUTOMATIC REGI_CONTROL_TYPE : AUTOMATIC
PAGE_CONTROL_TYPE : AUTOMATIC THRESHOLD_TIME : 1
ECSFB_PER_UNIT_SEC_IN_NORMAL : 0 CALL_PER_UNIT_SEC_IN_NORMAL : 0
REGI_PER_UNIT_SEC_IN_NORMAL : 0 PAGE_PER_UNIT_SEC_IN_NORMAL : 1800
ECSFB_PER_UNIT_SEC_IN_MINOR : 25 CALL_PER_UNIT_SEC_IN_MINOR : 25
REGI_PER_UNIT_SEC_IN_MINOR : 50 PAGE_PER_UNIT_SEC_IN_MINOR : 25
ECSFB_PER_UNIT_SEC_IN_MAJOR : 20 CALL_PER_UNIT_SEC_IN_MAJOR : 20
REGI_PER_UNIT_SEC_IN_MAJOR : 40 PAGE_PER_UNIT_SEC_IN_MAJOR : 5
ECSFB_PER_UNIT_SEC_IN_CRITICAL : 15 CALL_PER_UNIT_SEC_IN_CRITICAL : 15
REGI_PER_UNIT_SEC_IN_CRITICAL : 30 PAGE_PER_UNIT_SEC_IN_CRITICAL : 1
UNIT_SECOND_INTERVAL : 3
RESULT = OK
COMPLETED
答案 0 :(得分:0)
我相信你想按组密钥对第二列进行分组。看看下面给出的代码是否有效。
var data = File
.ReadAllLines(textfiletest)
.Select(x => x.Split(':'))
.Where(x => x.Length > 1)
.GroupBy(x => x[0].Trim())
.ToDictionary(x => x.Key, x => x[1]);
答案 1 :(得分:0)
我从this帖子修改了@ Strilanc(感谢:) :)扩展功能:
public function store(InterestRequest $interest)
{
$interest = new Follower(array(
'user_id' => $interest->get('interest_id'),
'follower_id' => Auth::id()
));
$interest->save();
}
这是代码:
public static class Extensions
{
public static IEnumerable<T[]> Partition<T>(this IEnumerable<T> sequence, int partitionSize)
{
var buffer = new T[partitionSize];
var n = 0;
foreach (var item in sequence)
{
buffer[n] = item;
n += 1;
if (n == partitionSize)
{
yield return buffer;
buffer = new T[partitionSize];
n = 0;
}
}
//partial leftovers
if (n > 0) yield return buffer;
}
}
答案 2 :(得分:0)
问题是您必须先将每个组从每行分开,然后才能创建Dictionary
。看起来第一组的长度为42个字符,因此我们可以使用String.Substring()
将每个组分开。
每个群组拆分后,您可以使用Dictionary
Linq
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<string> fileLines = new List<string>
{
"MINOR_PSIST : 2 MAJOR_PSIST : 8 ",
"CRITICAL_PSIST : 60 ECSFB_CALL_CONTROL_TYPE : AUTOMATIC",
"CALL_CONTROL_TYPE : AUTOMATIC REGI_CONTROL_TYPE : AUTOMATIC",
"PAGE_CONTROL_TYPE : AUTOMATIC THRESHOLD_TIME : 1 ",
"ECSFB_PER_UNIT_SEC_IN_NORMAL : 0 CALL_PER_UNIT_SEC_IN_NORMAL : 0 ",
"REGI_PER_UNIT_SEC_IN_NORMAL : 0 PAGE_PER_UNIT_SEC_IN_NORMAL : 1800 ",
"ECSFB_PER_UNIT_SEC_IN_MINOR : 25 CALL_PER_UNIT_SEC_IN_MINOR : 25 ",
"REGI_PER_UNIT_SEC_IN_MINOR : 50 PAGE_PER_UNIT_SEC_IN_MINOR : 25 ",
"ECSFB_PER_UNIT_SEC_IN_MAJOR : 20 CALL_PER_UNIT_SEC_IN_MAJOR : 20 ",
"REGI_PER_UNIT_SEC_IN_MAJOR : 40 PAGE_PER_UNIT_SEC_IN_MAJOR : 5 ",
"ECSFB_PER_UNIT_SEC_IN_CRITICAL : 15 CALL_PER_UNIT_SEC_IN_CRITICAL : 15 ",
"REGI_PER_UNIT_SEC_IN_CRITICAL : 30 PAGE_PER_UNIT_SEC_IN_CRITICAL : 1 ",
"UNIT_SECOND_INTERVAL : 3 ",
"RESULT = OK",
"COMPLETED "
};
// Each group contains a colon
var groupLines = fileLines.Where(line => line.Contains(":"));
// Break out each group to its own line
List<string> groups = new List<string>();
foreach (string line in groupLines)
{
groups.Add(line.Substring(0, 42));
if (line.Length > 44)
{
groups.Add(line.Substring(44));
}
}
// Build the dictionary from the groups
Dictionary<string, string> dictionary = groups.Select(line => line.Split(':'))
.GroupBy(pieces => pieces[0].Trim(), pieces => pieces[1].Trim())
.ToDictionary(kvp => kvp.Key, kvp => kvp.First());
// Print the dictionary to verify success
foreach (var r in dictionary)
{
Console.WriteLine("Key: {0,-35} Value: {1}", r.Key, r.Value);
}
}
}
结果:
Key: MINOR_PSIST Value: 2
Key: MAJOR_PSIST Value: 8
Key: CRITICAL_PSIST Value: 60
Key: ECSFB_CALL_CONTROL_TYPE Value: AUTOMATIC
Key: CALL_CONTROL_TYPE Value: AUTOMATIC
Key: REGI_CONTROL_TYPE Value: AUTOMATIC
Key: PAGE_CONTROL_TYPE Value: AUTOMATIC
Key: THRESHOLD_TIME Value: 1
Key: ECSFB_PER_UNIT_SEC_IN_NORMAL Value: 0
Key: CALL_PER_UNIT_SEC_IN_NORMAL Value: 0
Key: REGI_PER_UNIT_SEC_IN_NORMAL Value: 0
Key: PAGE_PER_UNIT_SEC_IN_NORMAL Value: 1800
Key: ECSFB_PER_UNIT_SEC_IN_MINOR Value: 25
Key: CALL_PER_UNIT_SEC_IN_MINOR Value: 25
Key: REGI_PER_UNIT_SEC_IN_MINOR Value: 50
Key: PAGE_PER_UNIT_SEC_IN_MINOR Value: 25
Key: ECSFB_PER_UNIT_SEC_IN_MAJOR Value: 20
Key: CALL_PER_UNIT_SEC_IN_MAJOR Value: 20
Key: REGI_PER_UNIT_SEC_IN_MAJOR Value: 40
Key: PAGE_PER_UNIT_SEC_IN_MAJOR Value: 5
Key: ECSFB_PER_UNIT_SEC_IN_CRITICAL Value: 15
Key: CALL_PER_UNIT_SEC_IN_CRITICAL Value: 15
Key: REGI_PER_UNIT_SEC_IN_CRITICAL Value: 30
Key: PAGE_PER_UNIT_SEC_IN_CRITICAL Value: 1
Key: UNIT_SECOND_INTERVAL Value: 3