gpt4 book ai didi

C# Linq .ToDictionary() 键已存在

转载 作者:太空狗 更新时间:2023-10-29 22:14:26 33 4
gpt4 key购买 nike

最终编辑:我能够在 ini 文件中找到重复的字段。感谢大家的帮助!

我正在使用正则表达式解析 ini 文件,并使用 LINQ 将其存储在字典中:

Sample Data:
[WindowSettings]
Window X Pos='0'
Window Y Pos='0'
Window Maximized='false'
Window Name='Jabberwocky'

[Logging]
Directory='C:\Rosetta Stone\Logs'

编辑:这是实际导致问题的文件:http://pastebin.com/mQSrkrcP

EDIT2:我已将其缩小为由文件中的最后一部分引起:[list_first_nonprintable]

出于某种原因,我正在解析的其中一个文件抛出了这个异常:

System.ArgumentException: An item with the same key has already been added.

有什么方法可以让我找出导致问题的键(这样我就可以修复文件),或者跳过导致这个问题的键并继续解析?

代码如下:

try
{
// Read content of ini file.
string data = System.IO.File.ReadAllText(project);

// Create regular expression to parse ini file.
string pattern = @"^((?:\[)(?<Section>[^\]]*)(?:\])(?:[\r\n]{0,}|\Z))((?!\[)(?<Key>[^=]*?)(?:=)(?<Value>[^\r\n]*)(?:[\r\n]{0,4}))*";
//pattern = @"
//^ # Beginning of the line
//((?:\[) # Section Start
// (?<Section>[^\]]*) # Actual Section text into Section Group
// (?:\]) # Section End then EOL/EOB
// (?:[\r\n]{0,}|\Z)) # Match but don't capture the CRLF or EOB
// ( # Begin capture groups (Key Value Pairs)
// (?!\[) # Stop capture groups if a [ is found; new section
// (?<Key>[^=]*?) # Any text before the =, matched few as possible
// (?:=) # Get the = now
// (?<Value>[^\r\n]*) # Get everything that is not an Line Changes
// (?:[\r\n]{0,4}) # MBDC \r\n
// )* # End Capture groups";

// Parse each file into a Dictionary.
Dictionary<string, Dictionary<string, string>> iniFile
= (from Match m in Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)
select new
{
Section = m.Groups["Section"].Value,

kvps = (from cpKey in m.Groups["Key"].Captures.Cast<Capture>().Select((a, i) => new { a.Value, i })
join cpValue in m.Groups["Value"].Captures.Cast<Capture>().Select((b, i) => new { b.Value, i }) on cpKey.i equals cpValue.i
select new KeyValuePair<string, string>(cpKey.Value, cpValue.Value)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value)

}).ToDictionary(itm => itm.Section, itm => itm.kvps);

return iniFile;
}
catch (ArgumentException ex)
{
System.Diagnostics.Debug.Write(ex.ToString());
return new Dictionary<string, Dictionary<string, string>>();
}

提前致谢。

最佳答案

这只是意味着当您转换为字典时——

.ToDictionary(itm => itm.Section, itm => itm.kvps);

-- 有多个键(itm.Section)。您可以使用 ToLookup相反,它有点像字典,但允许多个键。

编辑

有几种方法可以调用 ToLookup。最简单的是指定键选择器:

var lookup = 
// ...
.ToLookup(itm => itm.Section);

这应该提供一个查询,其中键是 Group 类型。获取查找值应该返回一个 IEnumerable,其中 T 是匿名类型:

Group g = null;
// TODO get group
var lookupvalues = lookup[g];

如果 .NET 编译器不喜欢这样(有时它似乎无法确定各种类型应该是什么),您还可以指定一个元素选择器,例如:

ILookup<string, KeyValuePair<string,string>> lookup = 
// ...
.ToLookup(
itm => itm.Section.Value, // key selector
itm => itm.kvps // element selector
);

关于C# Linq .ToDictionary() 键已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10625879/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com