用于单独对象的 LINQ Compund FROM 子句

问题描述

YouTube 上或多或少(2020 年 1 月)有一段视频,解释了如何在 LINQ 中使用复合 where 子句。在此示例中,where 使用了两次,但用于不相关的对象。当我在 Visual Studio 2017 中(在 Program.cs 文件中)尝试此操作时,出现错误

Error: Predefined type 'System.ValueTuple`2' is not defined or imported

之前我收到了关于 foreach 子句的错误

示例在这里https://www.youtube.com/watch?v=uUsnDXYRADA&t=263s ,如下所示: int[] numbersA = {0,2,4,5,6,8,9}; int[] numbersB = {1,3,7,8};

var pairs = from a in numbersA
            from b in numbersB
            where a < b
            select (a,b);

Console.WriteLine("Pairs where a < b:");
foreach (var pair in pairs)
{
    Console.WriteLine($"{pair.a} is less than {pair.b}");
}

我有 VS Professional 2017,带有 ASP.NET Web 框架和工具 5.2.6、Core Razor 15.8、C# 工具 2.10 LINQ 在系统中有效,但此查询无效。

解决方法

这与 Linq 无关,您返回的是 Tuple 类型,您当前的框架版本不支持它。 .Net Framework 4.7+ 支持 Tuple。如果您升级项目以使用更高的框架版本 (4.7+),则代码将起作用。

More Information