Linq Query Syntax Return Type is not IGrouping

While testing linq queries I stumbled on an interesting error:


When using the group by clause using query syntax the return type is not “Ienumberable[IGrouping]”.
Its rather this:


The error is this:

Main.xaml: Cannot assign from type 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.IEnumerable`1[System.Int32]]' to type 'System.Collections.Generic.IEnumerable`1[System.Linq.IGrouping`2[System.Int32,System.Int32]]' in Assign activity 'Assign'.


However when using method query there is no error and the return type is correct:


@enesb Try this and let me know if its proper
(From x In arr
Group By x Into grp=Group
Select x).copyToDataTable

this is unrelated to my question. x has to be a datarow for “copyToDataTable” to work.

I always hate the FROM Syntax, I mean in general the VB.NET Syntax is pretty poor compared to C#

Since the method syntax clearly works, and I think we can usually trust the compiler, I would hazard a guess the FROM Syntax is wrong, you sure you need the SELECT grp at the end? That feels like a Select() query at the end which would turn it into an iEnumerable.

Yes the SELECT is not necessary but if I omit the SELECT at the end the error turns into this:

Error	ERROR	Validation Error	BC36754: 'IEnumerable(Of <anonymous type: Key a As Integer, Key grp As IEnumerable(Of Integer)>)' cannot be converted to 'IEnumerable(Of IGrouping(Of Integer, Integer))' because '<anonymous type: Key a As Integer, Key grp As IEnumerable(Of Integer)>' is not derived from 'IGrouping(Of Integer, Integer)', as required for the 'Out' generic parameter 'T' in 'Interface IEnumerable(Of Out T)'. The selected value is incompatible with the property type.	Main.xaml

The compiler cant infer the IGROUPING when query syntax is used for some reason. I also tried with a c# project and the same error occurs.

EDIT: In the c# version of the query it actually works. The compiler can infer IGROUPING. So vb is the culprit.

Another argument in my opinion to skip the Query syntax :sweat_smile:
But yeah, then that Select is changing the data type so the compiler is correct to fuss.

I am sure if you cast the data type it could solve the issue in Query Syntax, but I’d still argue to use the method syntax, which imo is better. Personal preference of course.

1 Like

Yes thank you for guiding me in the right direction. I just tested the c# query again and it worked. Do you know why the SELECT clause changes the type in VB? In C# using select or omitting it doesnt change the data type at all Its IGROUPING in both cases.

Hi @enesb

Wouldn’t it be expected, given the return types as per the documentation?

GroupBy<TSource,TKey,TResult>(IEnumerable, Func<TSource,TKey>, Func<TKey,IEnumerable,TResult>):

GroupBy<TSource,TKey>(IEnumerable, Func<TSource,TKey>):

The Compiler Message is reasonable
The Use case cannot be derived from LINQ or description

It Looks also Like the distinct int32 items from Array are expected. Which we can do

When Something additional is to do with the group members, then please share with us Input Samples and expected output

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.