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();