我试图在列表中只得到奇怪的参数.这是我的一段代码
static void Main(string[] args)
{
var swictches = args.ToList().Select((x, index) => (index % 2) == 1);
var ss = swictches[0];
string test = doc.ReadAllPages();
Console.WriteLine(test.Substring(0, 1000));
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
在参数列表中,它具有开关和参数.我想要获得所有的开关.当我运行此代码时,开关变量如下所示:
false
true
false
Run Code Online (Sandbox Code Playgroud)
而不是像这样
-i
-awq
-l
Run Code Online (Sandbox Code Playgroud)
使用Where而不是Select:
var swictches = args.Where((x, index) => (index % 2) == 1).ToList();
Run Code Online (Sandbox Code Playgroud)
此外,您不必打电话ToList()使用Where/ Select.string[]也实现IEnumerable<string>了,所以你可以在它上面使用LINQ.而不是ToList在开头调用它作为最后一个方法,将结果实例化为List<string>.
编辑:
正如评论中指出的那样 您应该First在需要序列中的第一个元素时使用,而不是在结果上调用ToList()和使用[0].它会更快:
var ss = args.Where((x, index) => (index % 2) == 1).First();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |