It is not possible to convert a lambda expression to type "string" because it is not a delegate type - OrderBy and DbGeography by ref

Error with error:

private IQueryable<Field> PrepareAllFieldsQuery( ref DbGeography geo, int page, int amount, string sort, string order, ISearchCriteria searchCriteria ) { var query = this.context.Fields .Where( x => x.DeletedAt == null ) .OrderBy( x => x.GeoLocation.Distance( geo ) ); ... } 

It works great

 private IQueryable<Field> PrepareAllFieldsQuery( DbGeography geo, int page, int amount, string sort, string order, ISearchCriteria searchCriteria ) { var query = this.context.Fields .Where( x => x.DeletedAt == null ) .OrderBy( x => x.GeoLocation.Distance( geo ) ); ... } 

The difference is that my DbGeography did not DbGeography ref this time.

Any reasons why the .OrderBy( x => x.GeoLocation.Distance( geo ) ); function .OrderBy( x => x.GeoLocation.Distance( geo ) ); would produce the following error:

Cannot convert lambda expression to type 'string' because it is not a delegate type

+5
source share
1 answer

The error you get is caused by the funny things that C # overload resolution does. She is trying to resolve your OrderBy for the Dynamic Linq "version" of OrderBy , which takes a string as parameter.

In general, the more correct error and the one you get if you delete using System.Linq.Dynamic; , or if you are rewriting a statement to force using Queryable.OrderBy , for example:

 var query = Queryable.OrderBy( this.context.Fields.Where( x => x.DeletedAt == null ), x => x.GeoLocation.Distance( geo ) ); 

You cannot use the ref or out "geo" parameter inside an anonymous method, lambda expression, or query expression. As rdoubleui pointed out, there is an explanation here .

+4
source

All Articles