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

ParseException: "Type not found" for casting 'it' #846

Closed
Wasserwecken opened this issue Oct 15, 2024 · 1 comment
Closed

ParseException: "Type not found" for casting 'it' #846

Wasserwecken opened this issue Oct 15, 2024 · 1 comment
Assignees
Labels

Comments

@Wasserwecken
Copy link

1. Description

I want to filter for a property that is declared on a derived class. But the IQueryable is defined as the base class.
Unfortunatly, I cannot figure out how to cast the elements to the derived class. I tried the casting as suggested in this issue, but it does not work for me: #452 (comment).
I would like to avoid the 'CustomTypeProvider' and I have no issue with fully qualify the type names.

image

2. Fiddle or Project

Heres my attempt of casting...

using ConsoleApp1.App.Foo.Bar;
using System.Linq.Dynamic.Core;


// Holds a mix of derivatives of 'BaseEntity'
var query = Enumerable.Empty<BaseEntity>().AsQueryable();

// This is what I want to generate :)
query.Where(e => ((SpecialEntity)e).Name == "Okay");

// Thats my attempt of casting...
var filter = $"{typeof(SpecialEntity)}(it).Name == \"Okay\"";
Console.WriteLine(filter);

query.Where(filter);


Console.WriteLine("Done!");
Console.ReadKey();


namespace ConsoleApp1.App.Foo.Bar
{
    public class BaseEntity
    {
        public required int Id { get; set; }
    }

    public class SpecialEntity
        : BaseEntity
    {
        public required string Name { get; set; }
    }
}
@StefH
Copy link
Collaborator

StefH commented Oct 15, 2024

In this case, you should use Cast.

query.Where("Cast(\"SpecialEntity\").Name == "Okay");

Maybe you also nee to use the full type for SpecialEntity

@StefH StefH closed this as completed Oct 15, 2024
@StefH StefH self-assigned this Oct 15, 2024
@StefH StefH added the question label Oct 15, 2024
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