LINQ 匿名对象由于其保护级别而无法访问

LINQ anonymous object is inaccessible due to its protection level

我有 LINQ 逻辑,可以将正则表达式分析的数据收集到动态创建的字典中。我面临的问题是访问字典中的数据 (Info)。

Regex
    .Matches(text, pattern,
        RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture) 
    .OfType<Match>()    
    .Select(mt => new
    {
        Info = mt.Groups["ADKey"].Captures
            .OfType<Capture>()
            .Select(cp => cp.Value)
            .Zip(mt.Groups["ADValue"].Captures.OfType<Capture>().Select(cp => cp.Value),
                (k, v) => new { key = k, value = v })
            .ToDictionary(cp => cp.key, cp => cp.value),
        Nodes = mt.Groups["Key"].Captures
            .OfType<Capture>()
            .Select(cp => cp.Value)
            .Zip(mt.Groups["Value"].Captures.OfType<Capture>().Select(cp => cp.Value),
                (k, v) => new { key = k, value = v })
            .ToDictionary(cp => cp.key, cp => cp.value),
    });

List<string> myInfo = new List<string>();
myInfo = Info.Keys.ToList();

当我尝试访问字典(信息)时,在转换为列表时,我得到了 "Info" 由于其保护级别而无法访问的错误。我该如何解决?

您当前正在创建一个匿名类型列表,其中有一个 属性、Info,即 Dictionary<string, string>。您似乎没有在变量中捕获结果。然后您尝试访问 Info ,就好像它是 RegEx 查询之外的单个对象一样。这两个 Info 引用了不同的东西。

我给你的第一个建议是像这样更改你的代码:

IEnumerable<Dictionary<string, string>> query =
    Regex
        .Matches(
            text,
            pattern,
            RegexOptions.IgnorePatternWhitespace
                | RegexOptions.ExplicitCapture) 
        .OfType<Match>()
        .Select(mt => mt.Groups["Key"].Captures
                .OfType<Capture>()
                .Select(cp => cp.Value)
                .Zip(
                    mt.Groups["Value"].Captures
                        .OfType<Capture>()
                        .Select(cp => cp.Value),
                    (k, v) => new
                    {
                        key = k,
                        value = v
                    })
                .ToDictionary(cp => cp.key, cp => cp.value));

现在,我认为您不想要 IEnumerable<Dictionary<string, string>>,因为在接下来的两行中您似乎想要直接从结果中提取密钥。

看来您可能想要这个:

Dictionary<string, string> query =
    Regex
        .Matches(text, pattern,
            RegexOptions.IgnorePatternWhitespace
                | RegexOptions.ExplicitCapture) 
        .OfType<Match>()
        .SelectMany(mt => mt.Groups["Key"].Captures
            .OfType<Capture>()
            .Select(cp => cp.Value)
            .Zip(
                mt.Groups["Value"].Captures
                    .OfType<Capture>()
                    .Select(cp => cp.Value),
                (k, v) => new
                {
                    key = k,
                    value = v
                }))
        .ToDictionary(cp => cp.key, cp => cp.value);

但我怀疑这会产生键冲突。

我想你可能想要这样的东西:

Match match = Regex.Match(text, pattern,
    RegexOptions.IgnorePatternWhitespace
        | RegexOptions.ExplicitCapture);

Dictionary<string, string> result =
    match
        .Groups["Key"]
        .Captures
        .OfType<Capture>()
        .Select(cp => cp.Value)
        .Zip(
            match.Groups["Value"].Captures
                .OfType<Capture>()
                .Select(cp => cp.Value),
            (k, v) => new
            {
                key = k,
                value = v
            })
        .ToDictionary(cp => cp.key, cp => cp.value);

那么你可以这样做:

    List<string> myInfo = result.Keys.ToList();

鉴于你更新的问题,在我看来你可能想要这个:

Match match =
    Regex.Match(text, pattern,
        RegexOptions.IgnorePatternWhitespace
            | RegexOptions.ExplicitCapture);

var result = new
{
    Info = match.Groups["ADKey"].Captures
        .OfType<Capture>()
        .Select(cp => cp.Value)
        .Zip(match.Groups["ADValue"].Captures.OfType<Capture>().Select(cp => cp.Value),
            (k, v) => new { key = k, value = v })
        .ToDictionary(cp => cp.key, cp => cp.value),
    Nodes = match.Groups["Key"].Captures
        .OfType<Capture>()
        .Select(cp => cp.Value)
        .Zip(match.Groups["Value"].Captures.OfType<Capture>().Select(cp => cp.Value),
            (k, v) => new { key = k, value = v })
        .ToDictionary(cp => cp.key, cp => cp.value),
};

List<string> myInfo = result.Info.Keys.ToList();

我遇到了类似的错误:

EnumerableExtensions.Join(IEnumerable, string)' is inaccessible due to its protection level

然后意识到我忘记在代码中包含 using System.Linq;