Linq Query does not return IEnumerable

I have the following Linq query, which is in a web application converted from .NET 1.1 to 3.5:

dim objListOfFilteredDataRows = from datarows as datarow in objDataSet.tables(0).rows _ where datarows("SomeColumn") = SomeValue 

I have the same request in an application that was created using .NET 3.5, and the request returns IEnumerable. However, the request in the converted application is returned:

 {Name = "WhereEnumerableIterator`1" FullName = "System.Linq.Enumerable+WhereEnumerableIterator`1[[System.Data.DataRow, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"} 

** Edit: When I select an expression, intellisense says that it does not know the type objListOfFilteredDataRows and assumes its type is "Object".

Why is this type not output in converted application 1.1, but output in "native" 3.5? **

What am I missing here? How to convert WhereEntereratorIterator`1 to IEnumerable? Thanks!

+4
source share
5 answers

I found this detailed explanation of the changes to the .NET project settings that need to be updated when converting the project from an earlier version of .NET to 3.5 so that the project and the compiler support Linq and the type inference function associated with using Generics. In the "Project Settings" section of the "Compile" tab, the "Option Selection" parameter should be set to "On". This will allow the compiler to infer the type "someobject" created by any "dim someobject = [some expression]". Therefore, when I used the debugger, I did not see the expected IEnumerable or could not use any of the associated members and properties of this interface. Since the project where the Linq query was located was the original 3.5 application, the "Define Parameter" option is enabled by default.

+3
source

No, you really got IEnumerable. This is simply implemented by the class inside the System.Linq.Enumerable namespace, a private class. This is inevitable; there is no way to instantiate an interface. Do not let the debugger information put you on the wrong track.

+5
source

The WhereEnumeratorIterator1 type is a generated class that itself implements IEnumerable(Of T) . Actually, this is the result of compiling the C # iterator into System.Core.dll.

No conversion is required because you never see the WhereEnumeratorIterator1 type in your code, since it is private to System.Core.Dll. Instead, you only see the type of the returned request, which in this case will be IEnumerable(Of T) .

+4
source

You can simply call the AsEnumerable() extension method for the query itself if you need an object explicitly as IEnumerable<T> (in case you need to mix LINQ-to-SQL and LINQ-to-Objects, for example).

For your example, this would be:

 dim objListOfFilteredDataRows = (from datarows as datarow in objDataSet.tables(0).rows _ where datarows("SomeColumn") = SomeValue).AsEnumerable() 

If you only need what IEnumerable<T> implements, then the request itself should work; are you having a problem?

+1
source

Make sure you have the System.Linq namespace in this file

0
source

Source: https://habr.com/ru/post/1299123/


All Articles