0

I have a class that I created dynamically. I dynamically create the properties of this class and add value to these properties. So far there is no problem. However, when I create a list of this class and make queries using "Microsoft.EntityFrameworkCore.DynamicLinq", I get an error. How can I create a dynamic class and query a list of this class type with "Microsoft.EntityFrameworkCore.DynamicLinq"? What's the best way to do this?

I have shared a similar code below. This code is giving an error. Can you guide me? Or I would be glad if there are examples you can share.

Error: System.InvalidOperationException: 'The binary operator GreaterThan is not defined for the types' System.Object 'and' System.Int32 '.'

        List<DynamicClass> schemas = new List<DynamicClass>();

        foreach (var item in Enumerable.Range(1,100))
        {
            Type schemaType = null;
            var props = new List<DynamicProperty>();

            props.Add(new DynamicProperty("Id", typeof(long)));
            props.Add(new DynamicProperty("Name", typeof(string)));
            props.Add(new DynamicProperty("Birthday", typeof(DateTime)));
            props.Add(new DynamicProperty("Age", typeof(int)));

            DynamicClass schemaClass = null;
            schemaType = DynamicClassFactory.CreateType(props);
            schemaClass = Activator.CreateInstance(schemaType) as DynamicClass;

            schemaClass.SetDynamicPropertyValue("Id", item);
            schemaClass.SetDynamicPropertyValue("Name", "Serkan");
            schemaClass.SetDynamicPropertyValue("Birthday", DateTime.Now.AddDays(item));
            schemaClass.SetDynamicPropertyValue("Age", DateTime.Now.Day + item);

            schemas.Add(schemaClass);
        }

        var query = schemas.AsQueryable().Where("(Id > @0 and Id < @1) or Id == @2", 10, 30, 31).OrderBy("Id"); // I'm getting the error I mentioned here
        var results = query.ToList();

1 Answer 1

0
    IList CreateListFor(object obj)
    {
        Type t = typeof(List<>).MakeGenericType(obj.GetType());
        return (IList)Activator.CreateInstance(t);
    }

    IList CreateListFor(Type t)
    {
        Type lt = typeof(List<>).MakeGenericType(t);
        return (IList)Activator.CreateInstance(lt);
    }

    void Test()
    {
        var props = new List<DynamicProperty>();

        props.Add(new DynamicProperty("Id", typeof(int)));
        props.Add(new DynamicProperty("Name", typeof(string)));
        props.Add(new DynamicProperty("Birthday", typeof(DateTime)));
        props.Add(new DynamicProperty("Age", typeof(int)));

        var schemaType = DynamicClassFactory.CreateType(props);

        var schemas = CreateListFor(schemaType);

        foreach (var itemx in Enumerable.Range(1, 100))
        {
            var schemaClass = Activator.CreateInstance(schemaType) as DynamicClass;

            schemaClass.SetDynamicPropertyValue("Id", itemx);

            schemaClass.SetDynamicPropertyValue("Name", "Serkan");

            schemaClass.SetDynamicPropertyValue("Birthday", DateTime.Now.AddDays(itemx));

            schemaClass.SetDynamicPropertyValue("Age", DateTime.Now.Day + itemx);

            schemas.Add(schemaClass);
        }

        var query = schemas.AsQueryable();
        var results = query.Where("(Id > @0 and Id < @1) or Id == @2", 10, 30, 31); // I'm getting the error I mentioned here
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.