4

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#?

14
  • @RahulSingh -- that doesn't help explain why it works in VB and not in C#. Why do I need to use AsEnumerable? Commented Nov 18, 2015 at 15:45
  • This one says I should be able to use it as I've used it without AsEnumerable() Commented Nov 18, 2015 at 15:46
  • 2
    @RahulSingh: No, you didn't. That other question is talking about LINQ to SQL, not datatables. Commented Nov 18, 2015 at 15:49
  • 2
    You need AsEnumerable() because DataTable doesn't implement IEnumerable<T> or IQueryable<T>. I don't know why it's working in VB without that, but you do need it in C#. Commented Nov 18, 2015 at 15:50
  • 1
    @roryap: No, it's not. The question is about LINQ to SQL, which doesn't typically use DataTables. Commented Nov 18, 2015 at 15:54

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good to know even the Skeetmaster can learn something new every day ;)
1

You should be good if you just changed

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

to:

var allRows = from row in ds.Tables[0].AsEnumerable() select row;

Evidently, VB is doing it implicitly but C# needs you to do it explicitly.

The reason is that DataSet.Tables is of type DataTableCollection which does not implement IEnumerable hence an explicit AsEnumerable() is needed.

Please let me know if it helped!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.