Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;无法将lambda表达式转换为类型';字符串';因为它不是委托类型;用C语言查询数据集#_C#_.net_Vb.net_Linq_Lambda - Fatal编程技术网

C# &引用;无法将lambda表达式转换为类型';字符串';因为它不是委托类型;用C语言查询数据集#

C# &引用;无法将lambda表达式转换为类型';字符串';因为它不是委托类型;用C语言查询数据集#,c#,.net,vb.net,linq,lambda,C#,.net,Vb.net,Linq,Lambda,我有一段代码,在VB.NET中编译得很好: Imports System Imports System.Data Imports System.Data.Entity Imports System.Data.SqlClient Imports System.Linq Imports System.Collections Imports System.Collections.Generic Friend Module MainModule Friend Sub Main(args As

我有一段代码,在VB.NET中编译得很好:

Imports System
Imports System.Data
Imports System.Data.Entity
Imports System.Data.SqlClient
Imports System.Linq
Imports System.Collections
Imports System.Collections.Generic

Friend Module MainModule
    Friend Sub Main(args As String())
        Dim ds = GetSqlDataSet("", "")
        Dim allRows = From row In ds.Tables(0) Select row
    End Sub

    Private Function GetSqlDataSet(ByVal forQuery As String,
                                   ByVal withConnectionString As String,
                                   ByVal ParamArray withParameters As SqlClient.SqlParameter()) As DataSet

        GetSqlDataSet = New DataSet()

        Using conn As New System.Data.SqlClient.SqlConnection(withConnectionString)
            Using command As New System.Data.SqlClient.SqlCommand(forQuery, conn)
                command.Parameters.AddRange(withParameters)

                Using dataAdaptor As New System.Data.SqlClient.SqlDataAdapter(command)
                    dataAdaptor.Fill(GetSqlDataSet)
                End Using
            End Using
        End Using
    End Function
End Module
以下是参考资料:

现在我有了一个在C#中看起来完全相同的东西:

以下是参考资料:

但我得到了一个错误:


我已经找到了很多关于和的资源,但显然我在这两种情况下都有这些资源。有人能帮我解释一下吗?为什么它在VB.NET中工作而不是在C#中工作,我如何让它在C#中工作?

看起来VB内置了对
数据表的支持。下面是一个简短但完整的示例:

Option Strict On

Imports System
Imports System.Data
Imports System.Linq

Public Class LinqTest

    Shared Sub Main()
        Dim table as DataTable = New DataTable
        Dim allRows = From row In table Select row
    End Sub

End Class

如果您编译它,然后对其使用
ildasm
,您会发现它正在自动调用
DataTableExtensions.AsEnumerable()
。我不想猜测在VB中指定的位置,但这不是C#自动执行的部分。只要在你的C代码中明确地做到这一点。

如果你只是做了些改变,你应该会做得很好

var allRows = from row in ds.Tables[0] select row;
致:

显然,VB是隐式的,但C#需要您显式地这样做

原因是DataSet.Tables是DataTableCollection类型,它不实现IEnumerable,因此需要显式的AsEnumerable()


请让我知道它是否有用

@RahulSingh——这无助于解释为什么它在VB中工作而不是在C#中工作。为什么我需要使用
AsEnumerable
?说我应该能够使用它,因为我使用它时没有
AsEnumerable()
@RahulSingh:不,你没有。另一个问题是LINQ到SQL,而不是datatables。您需要
AsEnumerable()
,因为
DataTable
没有实现
IEnumerable
IQueryable
。我不知道为什么没有它,它在VB中工作,但你确实需要它在C#中。@roryap:不,它不是。问题是关于LINQ到SQL,它通常不使用数据表。很高兴知道,即使是Skeetmaster也可以每天学习新的东西;)
var allRows = from row in ds.Tables[0] select row;
var allRows = from row in ds.Tables[0].AsEnumerable() select row;