开发者

Linq Query not returning IEnumerable

开发者 https://www.devze.com 2022-12-17 15:53 出处:网络
I have the follow Linq query that is in a web application that was converted from .NET 1.1 to 3.5: dim objListOfFilteredDataRows = from datarows as datarow in objDataSet.tables(0).rows _

I have the follow Linq query that is in a web application that was 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 exact same query in an application that was created using .NET 3.5 and the query returns an IEnumerable. However the query in the converted application is returning:

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

**Edit: Whe开发者_如何学JAVAn I highlight the expression, the intellisense says that it doesn't know the type of objListOfFilteredDataRows and assumes its a type of "Object".

Why is the type not infered in the converted 1.1 application but is infered in the "native" 3.5?**

What am I missing here? How do I convert the "WhereEnumeratorIterator`1 to an IEnumerable? Thanks!


No, you really did get an IEnumerable. It just happens to be implemented by a class inside the System.Linq.Enumerable namespace, a private class. That's unavoidable, there is no way to create an instance of an interface. Don't let the debugger info put you on the wrong track.


The type WhereEnumeratorIterator1 is a generated class which itself implements IEnumerable(Of T). It is actually the result of compiling a C# iterator into System.Core.dll.

No conversion is necessary because you never actually see the WhereEnumeratorIterator1 type in your code as it's private to System.Core.Dll. Instead you only see the return type of the query which in this case will be IEnumerable(Of T).


I found this detailed explaination of .NET project setting changes that must be updated when converting a project from an earlier version of .NET to 3.5 in order for the project and compiler to support Linq and the type inference feature associated with using Generics. In Project Settings, under the "Compile" tab, "Option infer" must be set to "On". This will allow the compiler to infer the type of "someobject" created by any "dim someobject = [some expression]" This is why when I was using the debugger I wasn't seeing the expected IEnumerable or able to use any of that interface's associated members and properties. Because the project that the Linq query was in was an original 3.5 application the "Option infer" setting is on by default.


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

For your example, it would be:

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

If all you need is something that implements IEnumerable<T>, then the query itself should work; are you experiencing an issue?


Please check that you have System.Linq namespace in this file

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号