Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot convert lambda expression to type 'string' because it is not a delegate type" querying dataset in C#

I have this code, which compiles fine in 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

Here are the references:

enter image description here

Now I have what looks like an exact equivalent in C#:

using System;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    internal static class MainEntryPoint
    {
        internal static void Main(string[] args)
        {
            var ds = GetSqlServerDataSet("", "");
            var allRows = from row in ds.Tables[0] select row;
        }

        public static System.Data.DataSet GetSqlServerDataSet(string usingQuery, 
            string usingConnectionString, params System.Data.SqlClient.SqlParameter[] withParameters)

        {
            var ret = new System.Data.DataSet();

            using (var conn = new System.Data.SqlClient.SqlConnection(usingConnectionString))
            {
                using (var command = new System.Data.SqlClient.SqlCommand(usingQuery, conn))
                {
                    command.Parameters.AddRange(withParameters);

                    using (var adapter = new System.Data.SqlClient.SqlDataAdapter(command))
                    {
                        adapter.Fill(ret);
                    }
                }
            }

            return ret;
        }
    }
}

And here are the references:

enter image description here

But I'm getting this error:

enter image description here

I've found numerous resources that speak of adding a reference/using statements for System.Linq and also System.Data.Entity, but obviously I have those in both cases. Can someone please help me shed some light on this? Why is it working in VB.NET and not C#, and how do I get it to work in C#?

like image 774
rory.ap Avatar asked Dec 25 '22 12:12

rory.ap


1 Answers

It looks like VB has built-in support for DataTable. Here's a short but complete example:

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

If you compile that and then use ildasm on it, you'll find it's calling DataTableExtensions.AsEnumerable() automatically. I wouldn't like to speculate about where that's specified in VB, but it's not part of what C# does automatically. Just do it explicitly in your C# code.

like image 56
Jon Skeet Avatar answered Dec 27 '22 19:12

Jon Skeet