Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectMany not work over JSON #858

Closed
changeforan opened this issue Dec 7, 2024 · 3 comments
Closed

SelectMany not work over JSON #858

changeforan opened this issue Dec 7, 2024 · 3 comments
Assignees
Labels

Comments

@changeforan
Copy link

changeforan commented Dec 7, 2024

1. Description

I want to use SelectMany over an object which is deserialized from a JSON but get this error:

Exception has occurred: CLR/System.ArgumentException
An unhandled exception of type 'System.ArgumentException' occurred in System.Linq.Expressions.dll: 'Expression of type 'System.Object' cannot be used for return type 'System.Collections.Generic.IEnumerable`1[System.Object]''

here is my code snip

using System.Linq.Dynamic.Core;
using Newtonsoft.Json;
var json = """
    [{
        "PhoneNumbers": [
            { "Number": "123" },
            { "Number": "456" }
        ]
    },
    {
        "PhoneNumbers": [
            { "Number": "789" },
            { "Number": "012" }
        ]
    }]
""";
var people = JsonConvert.DeserializeObject(json) as IEnumerable<dynamic> ?? [];
var numbsers = people.AsQueryable().SelectMany("PhoneNumbers").ToDynamicList();
Console.WriteLine(JsonConvert.SerializeObject(numbsers));

2. Exception

Exception message:
Exception has occurred: CLR/System.ArgumentException
An unhandled exception of type 'System.ArgumentException' occurred in System.Linq.Expressions.dll: 'Expression of type 'System.Object' cannot be used for return type 'System.Collections.Generic.IEnumerable`1[System.Object]''
Stack trace:
at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection`1 parameters, String paramName)
   at System.Linq.Expressions.Expression.Lambda(Type delegateType, Expression body, String name, Boolean tailCall, IEnumerable`1 parameters)
   at System.Linq.Expressions.Expression.Lambda(Type delegateType, Expression body, IEnumerable`1 parameters)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.SelectManyInternal(IQueryable source, ParsingConfig config, Type resultType, String selector, Object[] args)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.SelectMany(IQueryable source, ParsingConfig config, String selector, Object[] args)
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.SelectMany(IQueryable source, String selector, Object[] args)
   at Program.<Main>$(String[] args)

3. Fiddle or Project

Fiddle

4. Any further technical details

I found a temporary workaround is combining Select and LINQ like this:

public static IQueryable MySelectMany(this IQueryable source, string selector)
{
    return source.Select(selector).ToDynamicList()
        .Select(it => it as IEnumerable<object>)
        .SelectMany(it => it)
        .AsQueryable();
}
@StefH
Copy link
Collaborator

StefH commented Dec 7, 2024

#859

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2024

@changeforan
Native JSON support has been added, and I've also added the SelectMany.

However, I still need to upload the NuGet package.

@StefH StefH closed this as completed Dec 7, 2024
@StefH StefH self-assigned this Dec 7, 2024
@changeforan
Copy link
Author

hi @StefH, thanks for the quick fix, and I am wondering if this fix also works for an object like:

var people = new List<dynamic> {
    new {
        PhoneNumbers = new [] {
            new { Number = "123" },
            new { Number = "456" }
        }
    }
};

for now, SelectMany target PhoneNumbers also throws the same error.
But the tricky thing is that, if the people is init with Anonymous Type, it works:

var people = new [] {
    new {
        PhoneNumbers = new [] {
            new { Number = "123" },
            new { Number = "456" }
        }
    }
};

Do you have any idea why this would happen? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants