2010-06-03 40 views
2

我想学习一点LINQ,但是我马上就遇到了编译问题。有没有什么特定的原因,为什么这不起作用?C#排错中最简单的LINQ

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); 
      } 
     } 
    } 
} 

错误是:

找不到 'INT []' 为源类型的查询模式的实现。 '选择'未找到。您是否缺少对“System.Core.dll”的引用或“System.Linq”的使用指令?

+0

你是否尝试过使用列表而不是int []? – Lazarus 2010-06-03 14:54:18

+3

错误很明显。您是否缺少对System.Core.dll正确版本的引用?如果情况并非如此,它应该可以工作。 – 2010-06-03 14:54:41

回答

2

好吧,我们有一个错误信息,由微软员工仔细地写出来以便帮助,所以让我们来看看它。
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[]是一个基本的C#类型。基本的C#类型在哪里? System.Core程序。错误提到检查对System.Core的引用。那么,是否有对System.Core的引用?
要检查此项:
解决方案资源管理器 - >参考。在列表中,您是否看到System.Core?
如果不是,很奇怪,但它很容易添加。右键单击引用,查看.NET for System.Core,添加它,然后瞧。

+0

'int []'不在'System.Core'中。 LINQ扩展方法是。 – 2010-06-03 14:58:40

+0

小心分享它位于何处? – citronas 2010-06-03 16:56:12

+0

@Matti:不是System.Core CLR的基础?还是那个mscorlib? – Rubys 2010-06-03 18:16:40

2

您是否在项目中引用了System.Core?其他一切都是正确的。