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

Cannot compare 'Object' type #451

Closed
justinushermawan opened this issue Nov 12, 2020 · 6 comments
Closed

Cannot compare 'Object' type #451

justinushermawan opened this issue Nov 12, 2020 · 6 comments
Assignees
Labels

Comments

@justinushermawan
Copy link

justinushermawan commented Nov 12, 2020

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'.

@zspitz
Copy link

zspitz commented Nov 12, 2020

@justinushermawan As a workaround, you could do the following:

var result = queryable.Where("Values[1].Equals(\"Yeremia\") || Values[2].Equals(24)").ToList();

@justinushermawan
Copy link
Author

Thank you. Yes, I think it would be possible to call the Object.Equals() instead .

@zspitz
Copy link

zspitz commented Nov 15, 2020

@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.

@igitur
Copy link
Contributor

igitur commented Apr 20, 2021

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

@StefH
Copy link
Collaborator

StefH commented Apr 26, 2024

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

@StefH StefH closed this as completed Apr 26, 2024
@StefH
Copy link
Collaborator

StefH commented Apr 26, 2024

#805

@StefH StefH reopened this May 5, 2024
@StefH StefH closed this as completed Jun 26, 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

4 participants