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

IComparer<T> not supported for OrderBy? #548

Closed
NetMage opened this issue Oct 12, 2021 · 8 comments
Closed

IComparer<T> not supported for OrderBy? #548

NetMage opened this issue Oct 12, 2021 · 8 comments
Labels

Comments

@NetMage
Copy link

NetMage commented Oct 12, 2021

How can the OrderBy overloads that take an IComparer actually be used, since the calls generated expect the types passed to the comparer match the types of the dynamic fields created by the string expression?

Is there any way to have an IComparer<object> be used?

@StefH StefH added the question label Dec 18, 2021
@StefH
Copy link
Collaborator

StefH commented Dec 18, 2021

Hello @NetMage,

The IComparer is supported, a unit test can be found here:
https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/test/System.Linq.Dynamic.Core.Tests/QueryableTests.OrderBy.cs#L10

(IComparer<object> is compatible with IComparer)

@StefH StefH closed this as completed Dec 18, 2021
@NetMage
Copy link
Author

NetMage commented Dec 18, 2021

Except that test only works if the string for the field is actually the name of a string field. Try again with an integer field and your passed in IComparer object will be the wrong type.

@StefH StefH reopened this Dec 19, 2021
@StefH
Copy link
Collaborator

StefH commented Dec 19, 2021

@maxime-ducoroy
Copy link

I have the same problem,
I have some code similar to the link you sent (QueryableTests.OrderBy.cs#L10)
For information I use .NET 6, EF Core 6.0.3, and a PostgresSQL database.
Here is my code:

query.OrderBy($"{columnName} {(sorting.Descendant ? "desc" : "asc")}", StringComparer.OrdinalIgnoreCase)

The error in visual studio when I run the code

The LINQ expression 'DbSet<Reference>()
    .Include(_ => _.Warehouse)
    .Include(_ => _.Departments)
    .Include(_ => _.Nature)
    .OrderBy(
        keySelector : => .Nature.Description, 
        compare : __p_0)' 
could not be translated. Either rewrite the query in a form that can be translated, 
or explicitly switch to client evaluation by inserting
 a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList' or 'ToListAsync'. 
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Can you please help me?

@StefH
Copy link
Collaborator

StefH commented Apr 17, 2022

@NetMage Did you have time to look at my new / updated UnitTests ?

@maxime-ducoroy Maybe it's related to Postgress database? Did you try SQL ? Or can you provide a full working example project?

@i-am-jyotirmaya
Copy link

i-am-jyotirmaya commented May 26, 2022

OrderBy accepts a IComparer but it have to implement both IComparer and IComparer<object>. It would look something like,

public class CustomComparer : IComparer<object>, IComparer
{
	public int Compare(object x, object y)
	{
		// Compare here
	}
}

@StefH StefH changed the title IComparer not supported for OrderBy? IComparer<T> not supported for OrderBy? Jun 7, 2022
@StefH
Copy link
Collaborator

StefH commented Jun 7, 2022

@i-am-jyotirmaya
I understand your issue.

I've changed the code so that you can also provide an IComparer<T> to the order extension method:

 var orderByDynamic = qry.OrderBy("Income", new ObjectComparer()).ToArray();

@StefH
Copy link
Collaborator

StefH commented Jun 7, 2022

#584

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