Cannot compare 'Object' type
Hi, does it support for 'object' data type?
public class Index
{
public long Position { get; set; }
public object[] Values { get; set; }
}
var indexes = new Dictionary<long, Index>();
indexes.Add(1, new Index { Position = 1000, Values = new object[3] { "Welly", "Chandra", 26 } });
indexes.Add(2, new Index { Position = 1001, Values = new object[3] { "Darma", "Angelo", 25 } });
indexes.Add(3, new Index { Position = 1002, Values = new object[3] { "Abby", "Yeremia", 28 } });
indexes.Add(4, new Index { Position = 1003, Values = new object[3] { "Yonathan", "Gunawan", 22 } });
indexes.Add(5, new Index { Position = 1004, Values = new object[3] { "Aldy", "Santoso", 24 } });
var queryable = indexes.Values.AsQueryable();
var result = queryable.Where("Values[1] = \"Yeremia\" || Values[2] = 24").ToList();
It always throw an exception:
System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Object' and 'System.Int32'.
@justinushermawan As a workaround, you could do the following:
var result = queryable.Where("Values[1].Equals(\"Yeremia\") || Values[2].Equals(24)").ToList();
Thank you. Yes, I think it would be possible to call the Object.Equals() instead .
@StefH @JonathanMagnan
Could this issue be fixed by checking if there is an operator overload for = between the two sides, and if not then replacing with a call to the .Equals instance method? There's already a similar check for implicit conversions between two types.
I bumped into this as well. Here is my use-case:
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Collections.Generic;
internal class Program
{
private class Person
{
// Deliberately typing these as `object` to illustrate the issue
public object Id { get; set; } = Guid.NewGuid();
public object Name { get; set; }
public object Age => Convert.ToInt32(Math.Floor((DateTime.Today.Month - DateOfBirth.Month + 12 * DateTime.Today.Year - 12 * DateOfBirth.Year) / 12d));
public DateTime DateOfBirth { get; set; }
}
private static void Main(string[] args)
{
var people = new List<Person>
{
new Person {Name = "Francois", DateOfBirth = new DateTime(1978, 07, 16)},
new Person {Name = "Marichen", DateOfBirth = new DateTime(1982, 07, 28)},
new Person {Name = "Wynand", DateOfBirth = new DateTime(1977, 01, 01)},
new Person {Name = "Theron", DateOfBirth = new DateTime(1972, 06, 15)},
new Person {Name = "Werner", DateOfBirth = new DateTime(1998, 02, 28)},
new Person {Name = "Graeme", DateOfBirth = new DateTime(1935, 09, 03)},
};
var younglings = people
.AsQueryable()
.Where("Age < 30")
.ToList();
foreach (var p in younglings)
Console.WriteLine($"{p.Name} {p.Age}");
Console.WriteLine();
Console.WriteLine("====================================");
Console.WriteLine();
Console.ReadKey(false);
}
}
@igitur @justinushermawan
This will be supported in the next version by setting a config setting (ConvertObjectToSupportComparison) to true.
The next code will work:
class Person
{
public string Name { get; set; }
public object Age { get; set; }
}
var persons = new[]
{
new Person { Name = "Foo", Age = 99 },
new Person { Name = "Bar", Age = 33 }
}.AsQueryable();
var config = new ParsingConfig
{
ConvertObjectToSupportComparison = true
};
var results = persons.Where(config, "Age > 50").ToList();
#805