0

I recently upgraded my VS2015 project to VS2017. I upgraded the .NET framework to 4.7.2 but now I get the following problem

    using (IDataReader rd = ExecuteCmdAndGetReader(myssqlstring))
    {
        MyList = rd.Select<myObject>(myObject.Load).ToList();
    }

    internal SqlDataReader ExecuteCmdAndGetReader(string sql)
    {
        SqlCommand cmd;
        cmd = new SqlCommand(sql, connection);
        SqlDataReader reader = cmd.ExecuteReader();
        return reader;
    }

The error is

'IDataReader' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'IDataReader' could be found (are you missing a using directive or an assembly reference?)

I do have System.Linq in uses and System.Core in References. Any ideas please?

user247702
  • 23,641
  • 15
  • 110
  • 157
user2837961
  • 1,505
  • 3
  • 27
  • 67
  • Do you have a reference to Microsoft.VisualStudio.Data.dll? – user82593 Sep 03 '18 at 07:52
  • Is ExecuteCmdAndGetReader a function you have written? – Neil Sep 03 '18 at 07:53
  • 2
    You can try creating extension method for `Select` with `IDataReader` as suggested [here](https://stackoverflow.com/a/1464929). Of course current LINQ `Select` doesn't have native support for `IDataReader`. – Tetsuya Yamamoto Sep 03 '18 at 07:54
  • 1
    `IDataReader` doesn't have a `Select` function. Is this an extension function you have created for your project and you don't have the correct usings in place to pull it in? https://learn.microsoft.com/en-us/dotnet/api/system.data.idatareader?view=netframework-4.7.2 – Neil Sep 03 '18 at 07:55
  • 2
    Your question seems to imply that in previous version of Net this code worked. Is this right? – Steve Sep 03 '18 at 07:55
  • Yes. In previous (VS2015) it was working fine – user2837961 Sep 03 '18 at 07:57
  • Then you are missing something in your project because Select is not a method for IDbDataReader even in previous versions of NET. Look at the comment from @Neil – Steve Sep 03 '18 at 07:58
  • @user2837961 No, probably you had your own extension method that you just forgot to reference in the new code. – Magnus Sep 03 '18 at 07:59
  • Thanks all. I shall implement my extension class as below. – user2837961 Sep 03 '18 at 08:01
  • 3
    Hang on, @user2837961 you said it was previously working. You need to find out why it no longer works, rather than just 'fixing it'. Your previous implementation may have some edge-case code in it to make your project work. You need to do some more due-diligence before randomly taking code off the internet ! – Neil Sep 03 '18 at 08:05
  • Yes @Neil. I would do that instead of blindly copying code. I will have to revert lot of projects back to VS2015 and work out why. – user2837961 Sep 03 '18 at 08:11

1 Answers1

5

IDataReader doesn't implement the IEnumerable interface, hence no Select etc. LINQ methods. I suggest you could implement your own Select extension method:

public static IEnumerable<TResult> Select<TResult>(this IDataReader reader,
                                       Func<IDataReader, TResult> selector)
{
    while (reader.Read())
    {
        yield return selector(reader);
    }
}
Vladi Pavelka
  • 916
  • 4
  • 12