简体   繁体   English

无法将X汇总到Count()

[英]Aggregate X Into Count() is not accessible

I'm looking at a solution that began life in .net Framework 2.0 and has now been moved up to Framework 4.0, so I'm probably missing a required reference and/or Imports line somewhere, I think... 我正在寻找一个始于.net Framework 2.0的解决方案,现在已经升级到Framework 4.0,所以我认为我可能在某处缺少必需的参考和/或Imports行。

I've got a LINQ query in the form: 我有以下形式的LINQ查询:

Dim x = Aggregate myDataRow As System.Data.DataRow In myDataTable _
        Where (booleanCondition1) _
        AndAlso (booleanCondition2) _
        AndAlso (booleanCondition3) _
        Into Count()

I'm getting the compile error " Definition of method 'Count' is not accessible in this context ". 我收到编译错误“ Definition of method 'Count' is not accessible in this context ”。

As far as I can tell from the literature the Count() function should be available (and apparently should register as a keyword). 至于我可以告诉从文献伯爵()函数应该是可用的(显然应该作为关键字登记)。

What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

I've just changed my code to a straight Select method, like so: 我刚刚将代码更改为直接的Select方法,如下所示:

Dim x = (From myDataRow As System.Data.DataRow In myDataTable _
        Where (booleanCondition1) _
        AndAlso (booleanCondition2) _
        AndAlso (booleanCondition3) _
        Select myDataRow)

and when I try to get the .Count of x, I get the message " 'Count' is not a member of 'System.Data.EnumerableRowCollection(Of System.Data.DataRow) " and according to the MSDN, it is . 当我尝试获取x的.Count时,收到消息“ 'Count' is not a member of 'System.Data.EnumerableRowCollection(Of System.Data.DataRow) ”,并且根据MSDN,它是

Totally confused, now. 现在完全困惑了。

First, let's look at EnumerableRowCollection(Of TRow) , and note that Count is an extension method: 首先,让我们看一下EnumerableRowCollection(Of TRow) ,注意Count是一个扩展方法:

Count<TRow>() Overloaded. Count<TRow>()重载。 Returns the number of elements in a sequence. 返回序列中的元素数。 (Defined by Enumerable .) (由Enumerable定义。)

Enumerable itself has this information: Enumerable本身具有以下信息:

Namespace: System.Linq 命名空间: System.Linq
Assembly: System.Core (in System.Core.dll) 程序集: System.Core(在System.Core.dll中)

Note that you'll always find information on both assemblies and namespaces in the MSDN help. 请注意,您将始终在MSDN帮助中找到有关程序集和名称空间的信息。 Both are important, and there's not a one-one correspondence between a type's namespace and the assembly that it's found in. 两者都很重要,并且类型的名称空间与其所在的程序集之间没有一一对应的关系。

So, next, let's look at extension methods . 接下来,让我们看看扩展方法 Unfortunately, it says this: 不幸的是,它说:

Typically, the module in which an extension method is defined is not the same module as the one in which it is called. 通常,定义扩展方法的模块与调用扩展方法的模块不同。 Instead, the module that contains the extension method is imported, if it needs to be, to bring it into scope 相反,如果需要,将导入包含扩展方法的模块以将其纳入范围

and then doesn't really elaborate fuller on what it means for the module to be in scope. 然后并没有详细阐述该模块在范围内的含义。 It does hint at it towards the end though: 它确实暗示了它的结尾:

When two extension methods that have identical signatures are in scope and accessible, the one with higher precedence will be invoked. 当两个具有相同签名的扩展方法在范围内并且可访问时,将调用优先级更高的方法。 An extension method's precedence is based on the mechanism used to bring the method into scope. 扩展方法的优先级基于将方法纳入范围的机制。 The following list shows the precedence hierarchy, from highest to lowest. 以下列表显示了从最高到最低的优先级层次结构。

  • Extension methods defined inside the current module. 当前模块内部定义的扩展方法。

  • Extension methods defined inside data types in the current namespace or any one of its parents, with child namespaces having higher precedence than parent namespaces. 在当前名称空间或其任何父名称的数据类型内部定义的扩展方法,子名称空间的优先级高于父名称空间。

  • Extension methods defined inside any type imports in the current file. 在任何类型内定义的扩展方法都将导入当前文件。

  • Extension methods defined inside any namespace imports in the current file. 在当前文件的任何名称空间导入中定义的扩展方法。

  • Extension methods defined inside any project-level type imports. 在任何项目级类型导入中定义的扩展方法。

  • Extension methods defined inside any project-level namespace imports. 在任何项目级名称空间导入中定义的扩展方法。

Since the first two bullets aren't relevant to your situation, only the remainder apply. 由于前两个项目符号与您的情况无关,因此仅其余项目适用。 And they all deal with imports 他们都处理进口

Imports : Imports

The Imports statement enables types that are contained in a given namespace to be referenced directly. 通过Imports语句,可以直接引用给定名称空间中包含的类型。

Hopefully, from all of the above, you can see why having a reference to the correct assembly, by itself, was insufficient, and you also needed to add an import. 希望从上述所有内容中,您可以看到为什么仅对正确的程序集进行引用是不够的,并且还需要添加导入。

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

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