简体   繁体   English

C#故障排除中最简单的LINQ

[英]Simplest LINQ in C# troubleshooting

I'm trying to learn a bit of LINQ but I'm having compile issues right off the bat. 我正在尝试学习LINQ,但是马上就遇到了编译问题。 Is there any specific reason why this won't work? 是否有任何特定原因导致此方法无效?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text;  


namespace HelloLINQ {

    class HelloLINQ
    {
        public static void Main()
        {
            Example1();
        }


        public static void Example1()
        {
            var numbers = new int[] { 1, 5, 3, 7, 3, 8, 9, 3, 6, 6, 2 };
            var under5 = from n in numbers
                         select n;
            foreach (var n in under5)
            {
                Console.WriteLine(n);
            }
        }
    } 
}  

The error is: 错误是:

Could not find an implementation of the query pattern for source type 'int[]'. 找不到源类型“ int []”的查询模式的实现。 'Select' not found. 找不到“选择”。 Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'? 您是否缺少对“ System.Core.dll”的引用或对“ System.Linq”的using指令?

Do you have a reference to System.Core in your project? 您的项目中是否有对System.Core的引用? Everything else is correct. 其他一切都是正确的。

Well, we have an error message, carefully written by a Microsoft employee to be helpful, so let's look at it. 好吧,我们有一条错误消息,由Microsoft员工精心撰写,可以为您提供帮助,因此让我们来看一下。
Could not find an implementation of the query pattern for source type 'int[]'. 'Select' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?
int[] is a basic C# type. int[]是基本的C#类型。 Where are the basic C# types located? 基本的C#类型位于哪里? System.Core. System.Core程序。 And the error mentions checking for a reference to System.Core. 该错误提到检查对System.Core的引用。 So, is there a reference to System.Core? 那么,是否有对System.Core的引用?
To check this: 要检查此:
Solution Explorer->References. 解决方案资源管理器->参考。 In the list, do you see System.Core? 在列表中,您是否看到System.Core?
If not, weird, but it's easily addable. 如果不是,那很奇怪,但是它很容易添加。 Right click on references, look under .NET for System.Core, add it, and voila. 右键单击引用,在.NET下查找System.Core,添加它,然后瞧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM