我可以将LINQ查询结果存储在数组中吗?

这是使用LINQ的代码,它将值存储到列表中

string StringRegex = "\"(?:[^\"\\\\]|\\\\.)*\"";

        Dictionary<string, string> dictionaryofString = new Dictionary<string, string>()
        {
            {"String", StringRegex}
        };

 var matches = dictionaryofString.SelectMany(a => Regex.Matches(input,a.Value)
        .Cast<Match>()
        .Select(b =>
                new
                {
                    Index = b.Index,
                    Value = b.Value,
                    Token = a.Key
                }))
        .OrderBy(a => a.Index).ToList();
        for (int i = 0; i < matches.Count; i++)
        {
            if (i + 1 < matches.Count)
            {
                int firstEndPos = (matches[i].Index + matches[i].Value.Length);
                if (firstEndPos > matches[(i + 1)].Index)
                {
                    matches.RemoveAt(i + 1);
                    i--;
                }
            }
        }
foreach (var match in matches)
        {
            Console.WriteLine(match);
        }

不能将其存储到Array中吗?在这里我只能显示我想要的项目。就像这里的输出是{Index =,Value =,Token =}同时,我希望不需要只是“ Value”“ Token”索引的输出。

马尔科·尤万奇奇(MarkoJuvančič)

您可以ToArray改用。但是List已经为您提供了所需的数组功能时,您可以通过其索引访问一个项目。

var exampleQuery = select c from componentsDemo;
var list = exampleQuery.ToList();
var secondElement = list[1]; // <- demo only, there could be an exception thrown, if there's less than two elements in the list

编辑:正如我从您的评论中看到的,您需要这样做:

foreach (var match in matches)
{
   Console.WriteLine(match.Value + ", " + match.Token); 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将 SQLAlchemy 查询的结果存储在数组中

PHP - 将查询结果存储在数组中

我可以将表单类存储在数组中吗?(德尔福)

我可以修改存储在数组中的输入的属性吗?

Java可以将方法存储在数组中吗?

我可以基于不在数组中的值查询吗?

将 for 循环的结果存储在数组中

我应该将函数的结果存储到数组中吗?

使用PHP将查询结果存储在数组中并在Javascript中使用数组

查询SQL表并将结果存储在数组中

我可以将时间序列数据存储在数据仓库中吗?

为什么我可以将大小不同的对象存储在数组中?

将void方法的结果存储在数组中(无效的转换)

是否可以将React组件存储在数组中?

我可以将mmap的结果存储为整数吗?

我可以使用JavaScript将Fetch的结果存储在全局变量中吗?

我可以将 HERE 地理编码 API 结果存储到数据库中吗?

我可以在数组中搜索对象的元素吗?

我应该将搜索结果存储在redux存储中吗?

将查询字符串存储在数据库中通常被认为可以吗?

我可以将查询结果投影到EF生成的模型中吗?

如何将linq查询的结果存储在KeyDictionary变量中

BigQuery:我可以通过将数据存储在多个表中来降低查询成本吗?

我可以在Scala案例匹配中存储中间结果吗?

我可以将UTF8存储在C型char数组中吗

我可以将重复的字符串存储在字符串数组中吗?

我可以将字符串存储在长度为1的char数组中吗

在数组中存储 .Find 结果

结果值存储在数组或 json 中