![](/img/trans.png)
[英]Can I assign the result of a Linq query to the source variable of the same query?
[英]Can I store LINQ query result in an array?
这是使用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);
}
不能将其存储到数组中吗? 在这里我只能显示我想要的项目。 就像这里的输出是{Index =,Value =,Token =}同时,我希望不需要只是“ Value”“ Token”索引的输出。
您可以改用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.